#4123 closed Bugs (invalid)
smatches contains wrong matches, access changes content
Reported by: | Owned by: | John Maddock | |
---|---|---|---|
Milestone: | Boost 1.43.0 | Component: | regex |
Version: | Boost Development Trunk | Severity: | Problem |
Keywords: | Cc: |
Description
$ cat main.cc #include <boost/regex.hpp> #include <iostream> int main(int argc, char **argv) { boost::regex expr(argv[1]); boost::smatch matches; if (boost::regex_search(std::string(argv[2]), matches, expr)) { for (size_t i = 0; i<matches.size(); ++i) std::cout << "XXX " << matches[i] << '\n'; for (size_t i = 0; i<matches.size(); ++i) std::cout << "YYY " << matches[i] << '\n'; for (size_t i = 0; i<matches.size(); ++i) std::cout << "ZZZ " << matches[i] << '\n'; } } $ ./main 'to ([a-z]+) [A-Z]+ ([0-9]+)' 'fs to hello WORLD 123' XXX to hello WORLD 123 XXX lo WO XXX 23 YYY hello WORLD 12323 YYY WORLD YYY 23 ZZZ lo WORLD 1232323 ZZZ LD 12 ZZZ 23
Expected output: three times: hello 123 (or at least three times the same output)
Perhaps I am using the API wrong, but I am inspired by this example(-output): /usr/share/doc/libboost1.38-doc/HTML/libs/regex/doc/html/boost_regex/captures.html
I get the same results with boost 1.38.
Using Ubuntu 9.10, x86/64, g++ 4.4.1.
Change History (2)
comment:1 by , 13 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
comment:2 by , 12 years ago
Thanks for the hint! What a stupid mistake. I posted this ticket way too fast ...
Note:
See TracTickets
for help on using tickets.
There is a bug in your code: you are passing a temporary std::string object to regex_search, so after the call the match_results object contains invalidated iterators (iterators into an object that has already been destroyed) - leading to garbage output.
HTH, John.