https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65470
Bug ID: 65470 Summary: regex_search corrupts matches when haystack is destroyed Product: gcc Version: 4.9.2 Status: UNCONFIRMED Severity: major Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: aral at gmx dot de Created attachment 35063 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=35063&action=edit minimal bug example - compile with g++ -std=c++11 regexbug.cpp -o regexbug Tested on g++ (Debian 4.9.2-10) 4.9.2. regex_search with matches apparently depends on the haystack (in both the const basic_string and const *charT versions) to remain intact. The matches object returned seems to point to locations in the original haystack. When the haystack is destroyed, the matches are corrupted. This leads to very unpleasant results when using regex_search with the temporary strings returned by .string() or .c_str() methods of many objects (e.g. boost::filesystem::path.filename() ), as those strings are destroyed at the end of the line containing the regex_search. Fix recommendation: 1) if this behavior is intended for performance, add a REALLY BIG FLASHING RED WARNING MESSAGE to the function documentation on cplusplus.com (and anywhere) that the haystack MUST NOT be a temporary string, and MUST be kept until after the matches have been evaluated. 2) to fix, modify the (sub)matches class so that it creates a copy of each match and manages that copy destruction itself To reproduce (also submitted as attachment): ----- #include <regex> #include <iostream> using namespace std; int main( void ) { cmatch match; // store the matches here, this object seems to depend on haystack after search char *haystack = strdup ("BUG DEMO"); regex_search( haystack, match, regex(".*") ); // perform regex search in the haystack, always matches, not checking match.size() for brevity cout << "correct match: "<< match[0] << endl; // document the correct match delete haystack; // destroy the haystack cout << "corrupt match: "<< match[0] << endl; // document the correct match return 0; } ----- (compile with g++ -std=c++11 regexbug.cpp -o regexbug)