diff options
author | Vadim Zeitlin <vz-swig@zeitlins.org> | 2010-08-14 14:12:23 +0000 |
---|---|---|
committer | Vadim Zeitlin <vz-swig@zeitlins.org> | 2010-08-14 14:12:23 +0000 |
commit | 024ed6ce2a3191c57c5c030dd133bf0e9e2a1971 (patch) | |
tree | b071adb6a3a881265340909be5efbe4ed009a3fd /Source | |
parent | 70b4d1231743911806b8a9625926187ce914560b (diff) | |
download | swig-024ed6ce2a3191c57c5c030dd133bf0e9e2a1971.tar.gz |
Fix bug in applying regex replacement to non-matching strings.
We didn't handle pcre_exec() return code properly and so the replacement could
be still done even if there was no match if the replacement part contained
anything else than back-references (in this, the only tested so far, case the
replacement was still done but the result turned out to be empty and the
calling code assumed the regex didn't match).
Do check for PCRE_ERROR_NOMATCH now and also give an error message if another
error unexpectedly occurred.
Add a test case for the bug that was fixed.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@12187 626c5289-ae23-0410-ae9c-e8d60b6d4f22
Diffstat (limited to 'Source')
-rw-r--r-- | Source/Swig/misc.c | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/Source/Swig/misc.c b/Source/Swig/misc.c index b3c647546..2105f0c51 100644 --- a/Source/Swig/misc.c +++ b/Source/Swig/misc.c @@ -1188,6 +1188,8 @@ String *Swig_string_regex(String *s) { int captures[30]; if (split_regex_pattern_subst(s, &pattern, &subst, &input)) { + int rc; + compiled_pat = pcre_compile( Char(pattern), pcre_options, &pcre_error, &pcre_errorpos, NULL); if (!compiled_pat) { @@ -1195,8 +1197,15 @@ String *Swig_string_regex(String *s) { pcre_error, Char(pattern), pcre_errorpos); exit(1); } - pcre_exec(compiled_pat, NULL, input, strlen(input), 0, 0, captures, 30); - res = replace_captures(input, subst, captures); + 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", + rc, Char(pattern), input); + exit(1); + } } DohDelete(pattern); |