Modify

Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#4123 closed Bugs (invalid)

smatches contains wrong matches, access changes content

Reported by: g_sauthoff@… 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.

Attachments (0)

Change History (2)

comment:1 Changed 8 years ago by John Maddock

Resolution: invalid
Status: newclosed

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 8 years ago by g_sauthoff@…

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

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The owner will remain John Maddock.
The resolution will be deleted.

Add Comment


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

 
Note: See TracTickets for help on using tickets.