Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#4123 closed Bugs (invalid)

smatches contains wrong matches, access changes content

Reported by: g_sauthoff@… Owned by: johnmaddock
Milestone: Boost 1.43.0 Component: regex
Version: Boost Development Trunk Severity: Problem
Keywords: Cc:


$ cat
#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 23
YYY hello WORLD 12323
YYY 23
ZZZ lo WORLD 1232323
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.

Attachments (0)

Change History (2)

comment:1 Changed 7 years ago by johnmaddock

  • Resolution set to invalid
  • Status changed from new to closed

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.

comment:2 Changed 7 years ago by g_sauthoff@…

Thanks for the hint! What a stupid mistake. I posted this ticket way too fast ...

Add Comment

Modify Ticket

Change Properties
Set your email in Preferences
as closed The owner will remain johnmaddock.
The resolution will be deleted. Next status will be 'reopened'.

E-mail address and user name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.