Fix crash when requesting more matches than available in regex

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@12904 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
William S Fulton 2012-01-27 20:55:32 +00:00
commit 29fd1e3ce4
2 changed files with 15 additions and 8 deletions

View file

@ -5,6 +5,9 @@ See the RELEASENOTES file for a summary of changes in each release.
Version 2.0.5 (in progress)
===========================
2012-01-24: wsfulton
Fix crash with bad regex - bug #3474250.
2012-01-24: wsfulton
[Python] Add Python stepped slicing support to the STL wrappers (std::vector, std::list).
Assigning to a slice, reading a slice and deleting a slice with steps now work.

View file

@ -1147,7 +1147,7 @@ err_out:
exit(1);
}
String *replace_captures(const char *input, String *subst, int captures[])
String *replace_captures(int num_captures, const char *input, String *subst, int captures[], String *pattern, String *s)
{
String *result = NewStringEmpty();
const char *p = Char(subst);
@ -1167,9 +1167,14 @@ String *replace_captures(const char *input, String *subst, int captures[])
Putc('\\', result);
} else if (isdigit((unsigned char)*p)) {
int group = *p++ - '0';
int l = captures[group*2], r = captures[group*2 + 1];
if (l != -1) {
Write(result, input + l, r - l);
if (group < num_captures) {
int l = captures[group*2], r = captures[group*2 + 1];
if (l != -1) {
Write(result, input + l, r - l);
}
} else {
Swig_error("SWIG", Getline(s), "PCRE capture replacement failed while matching \"%s\" using \"%s\" - request for group %d is greater than the number of captures %d.\n",
Char(pattern), input, group, num_captures-1);
}
}
}
@ -1206,10 +1211,9 @@ String *Swig_string_regex(String *s) {
}
rc = pcre_exec(compiled_pat, NULL, input, strlen(input), 0, 0, captures, 30);
if (rc >= 0) {
res = replace_captures(input, subst, captures);
}
else if (rc != PCRE_ERROR_NOMATCH) {
Swig_error("SWIG", Getline(s), "PCRE execution failed: error %d while matching \"%s\" in \"%s\".\n",
res = replace_captures(rc, input, subst, captures, pattern, s);
} else if (rc != PCRE_ERROR_NOMATCH) {
Swig_error("SWIG", Getline(s), "PCRE execution failed: error %d while matching \"%s\" using \"%s\".\n",
rc, Char(pattern), input);
exit(1);
}