Opened 15 years ago
Closed 15 years ago
#1749 closed Bugs (invalid)
regex_match on filesystem iterator.leaf () leads to strange results in what[]
| Reported by: | Owned by: | John Maddock | |
|---|---|---|---|
| Milestone: | Component: | regex | |
| Version: | Severity: | Problem | |
| Keywords: | filesystem path iterator | Cc: |
Description
Code fragment:
boost::smatch what;
boost::regex e ("^(tty:)(tty.+)$");
for (fs::directory_iterator itr_l2 (*itr_l1); itr_l2 != end_itr; ++itr_l2) {
...
match = boost::regex_match ((*itr_l2).leaf(), what, e);
leads to incorrect results in what[0], what[1]... when match occurs.
libboost_filesystem.so.1.33.1 libboost_regex.so.1.33.1 Suse 10.1
Note:
See TracTickets
for help on using tickets.

The problem here is that the leaf() function returns a *temporary string value*, so by the time that regex_match returns the string matched against has gone out of scope, and the iterators stored in match_results<> have all been invalidated. Assigning the result of leaf() to a local string object before calling regex_match will fix this.
HTH, John Maddock.