From 024ed6ce2a3191c57c5c030dd133bf0e9e2a1971 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 14 Aug 2010 14:12:23 +0000 Subject: [PATCH] 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 --- Examples/test-suite/rename_pcre_encoder.i | 7 ++++++- Source/Swig/misc.c | 13 +++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/Examples/test-suite/rename_pcre_encoder.i b/Examples/test-suite/rename_pcre_encoder.i index 568a2a82d..1bee1dca8 100644 --- a/Examples/test-suite/rename_pcre_encoder.i +++ b/Examples/test-suite/rename_pcre_encoder.i @@ -3,9 +3,14 @@ // strip the wx prefix from all identifiers except those starting with wxEVT %rename("%(regex:/wx(?!EVT)(.*)/\\1/)s") ""; +// Replace "Set" prefix with "put" in all functions +%rename("%(regex:/^Set(.*)/put\\1/)s", %$isfunction) ""; + %inline %{ -class wxSomeWidget { +struct wxSomeWidget { + void SetBorderWidth(int); + void SetSize(int, int); }; struct wxAnotherWidget { 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);