Browse Source

Importing kamailio fixes into the sip-router version of re.c

This patch imports the following two fixes:
 *  bug fixed in multiple matching on complete lines.
    Closes bug #1819248.
    Credits go to Alexander Bergolth (SF bergolth)
    (svn commit id 3096)

 * non-critical bug fixed - invalid regexps may lead to infinite matches
   and finally to memory starvation.
   Reported to Klaus Darilion <[email protected]>
   (svn commit id 1039)
Jan Janak 16 years ago
parent
commit
b23b15b997
1 changed files with 10 additions and 6 deletions
  1. 10 6
      re.c

+ 10 - 6
re.c

@@ -483,18 +483,20 @@ struct replace_lst* subst_run(struct subst_expr* se, const char* input,
 		DBG("subst_run: running. r=%d\n", r);
 		/* subst */
 		if (r==0){ /* != REG_NOMATCH */
-			/* change eflags, not to match any more at string start */
-			eflags|=REG_NOTBOL;
+			if (pmatch[0].rm_so==-1) {
+				ERR("subst_run: Unknown offset?\n");
+				goto error;
+			}
+			if (pmatch[0].rm_so==pmatch[0].rm_eo) {
+				ERR("subst_run: Matched string is empty, invalid regexp?\n");
+				goto error;
+			}
 			*crt=pkg_malloc(sizeof(struct replace_lst));
 			if (*crt==0){
 				LOG(L_ERR, "ERROR: subst_run: out of mem (crt)\n");
 				goto error;
 			}
 			memset(*crt, 0, sizeof(struct replace_lst));
-			if (pmatch[0].rm_so==-1){
-				LOG(L_ERR, "ERROR: subst_run: unknown offset?\n");
-				goto error;
-			}
 			(*crt)->offset=pmatch[0].rm_so+(int)(p-input);
 			(*crt)->size=pmatch[0].rm_eo-pmatch[0].rm_so;
 			DBG("subst_run: matched (%d, %d): [%.*s]\n",
@@ -507,6 +509,8 @@ struct replace_lst* subst_run(struct subst_expr* se, const char* input,
 			}
 			crt=&((*crt)->next);
 			p+=pmatch[0].rm_eo;
+			if (*(p-1) == '\n' || *(p-1) == '\r') eflags&=~REG_NOTBOL;
+			else eflags|=REG_NOTBOL;
 			cnt++;
 		}
 	}while((r==0) && se->replace_all);