#! /bin/sh /usr/share/dpatch/dpatch-run
## 80_fix_libcre3_max_sub_expression_allocation.dpatch by Russell Coker
##
## All lines beginning with `## DP:' are a description of the patch.
## DP: libpcre3 sets reg.re_nsub to an exreme large value and dansguardian
## DP: tries to allocate memory for all records which lets malloc fail.
## DP: this patch limits the allocation to 1024 records. (#667664)

@DPATCH@
diff -urNad '--exclude=CVS' '--exclude=.svn' '--exclude=.git' '--exclude=.arch' '--exclude=.hg' '--exclude=_darcs' '--exclude=.bzr' pkg-dansguardian~/src/RegExp.cpp pkg-dansguardian/src/RegExp.cpp
--- pkg-dansguardian~/src/RegExp.cpp	2011-10-29 14:16:31.000000000 +0200
+++ pkg-dansguardian/src/RegExp.cpp	2012-06-23 11:02:48.821473711 +0200
@@ -161,14 +161,17 @@
 	offsets.clear();
 	lengths.clear();
 	imatched = false;
-	regmatch_t *pmatch = new regmatch_t[reg.re_nsub + 1];  // to hold result
+	int num_sub_expressions = MAX_SUB_EXPRESSIONS;
+	if(reg.re_nsub < num_sub_expressions)
+		num_sub_expressions = reg.re_nsub;
+	regmatch_t *pmatch = new regmatch_t[num_sub_expressions + 1];  // to hold result
 	if (!pmatch) {  // if it failed
 		delete[]pmatch;
 		imatched = false;
 		return false;
 		// exception?
 	}
-	if (regexec(&reg, pos, reg.re_nsub + 1, pmatch, 0)) {  // run regex
+	if (regexec(&reg, pos, num_sub_expressions + 1, pmatch, 0)) {  // run regex
 		delete[]pmatch;
 		imatched = false;
 //        #ifdef DGDEBUG
@@ -182,7 +185,7 @@
 	int error = 0;
 	while (error == 0) {
 		largestoffset = 0;
-		for (i = 0; i <= (signed) reg.re_nsub; i++) {
+		for (i = 0; i <= (signed) num_sub_expressions; i++) {
 			if (pmatch[i].rm_so != -1) {
 				matchlen = pmatch[i].rm_eo - pmatch[i].rm_so;
 				submatch = new char[matchlen + 1];
@@ -199,7 +202,7 @@
 		}
 		if (largestoffset > 0) {
 			pos += largestoffset;
-			error = regexec(&reg, pos, reg.re_nsub + 1, pmatch, REG_NOTBOL);
+			error = regexec(&reg, pos, num_sub_expressions + 1, pmatch, REG_NOTBOL);
 		} else {
 			error = -1;
 		}
diff -urNad '--exclude=CVS' '--exclude=.svn' '--exclude=.git' '--exclude=.arch' '--exclude=.hg' '--exclude=_darcs' '--exclude=.bzr' pkg-dansguardian~/src/RegExp.hpp pkg-dansguardian/src/RegExp.hpp
--- pkg-dansguardian~/src/RegExp.hpp	2011-10-29 14:16:31.000000000 +0200
+++ pkg-dansguardian/src/RegExp.hpp	2012-06-23 11:02:48.821473711 +0200
@@ -22,6 +22,7 @@
 #ifndef __HPP_REGEXP
 #define __HPP_REGEXP
 
+#define MAX_SUB_EXPRESSIONS 1024
 
 // INCLUDES
 
