Title
regex_match()/regex_search() with match_results should forbid temporary strings
Status
c++14
Section
[re.syn]
Submitter
Stephan T. Lavavej

Created on 2013-09-21.00:00:00 last changed 131 months ago

Messages

Date: 2014-02-14.07:07:05

Proposed resolution:

This wording is relative to N3691.

  1. Edit [re.syn], header <regex> synopsis, as indicated:

    #include <initializer_list>
    
    namespace std {
    
      […]
      
      // 28.11.2, function template regex_match:
      […]
      template <class ST, class SA, class Allocator, class charT, class traits> 
      bool regex_match(const basic_string<charT, ST, SA>&&, 
                       match_results<
                         typename basic_string<charT, ST, SA>::const_iterator, 
                         Allocator>&, 
                       const basic_regex<charT, traits>&, 
                       regex_constants::match_flag_type = 
                         regex_constants::match_default) = delete;
    
      // 28.11.3, function template regex_search:
      […]
      template <class ST, class SA, class Allocator, class charT, class traits> 
      bool regex_search(const basic_string<charT, ST, SA>&&, 
                        match_results<
                          typename basic_string<charT, ST, SA>::const_iterator, 
                          Allocator>&, 
                        const basic_regex<charT, traits>&, 
                        regex_constants::match_flag_type = 
                          regex_constants::match_default) = delete;
      […]
    }
    
Date: 2014-02-13.00:00:00

[ 2014-02-13 Issaquah: Move as Immediate ]

Date: 2013-09-21.00:00:00

Consider the following code:

const regex r(R"(meow(\d+)\.txt)");
smatch m;
if (regex_match(dir_iter->path().filename().string(), m, r)) {
  DoSomethingWith(m[1]);
}

This occasionally crashes. The problem is that dir_iter->path().filename().string() returns a temporary string, so the match_results contains invalidated iterators into a destroyed temporary string.

It's fine for regex_match/regex_search(str, reg) to accept temporary strings, because they just return bool. However, the overloads taking match_results should forbid temporary strings.

History
Date User Action Args
2014-02-27 17:03:20adminsetstatus: wp -> c++14
2014-02-20 13:52:38adminsetstatus: immediate -> wp
2014-02-14 07:07:05adminsetmessages: + msg6856
2014-02-14 07:07:05adminsetstatus: new -> immediate
2013-10-12 18:21:48adminsetmessages: + msg6725
2013-09-21 00:00:00admincreate