Modify

Ticket #4123 (closed Bugs: invalid)

Opened 4 years ago

Last modified 4 years ago

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:

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

Change History

comment:1 Changed 4 years ago by johnmaddock

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

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

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

View

Add a comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
The resolution will be deleted. Next status will be 'reopened'
Author


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

 
Note: See TracTickets for help on using tickets.