Forums before death by AOL, social media and spammers... "We can't have nice things"
|    comp.lang.c++.moderated    |    Moderated discussion of C++ superhackery    |    33,346 messages    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
|    Message 32,892 of 33,346    |
|    Vyacheslav Kononenko to All    |
|    Using temporary string in regex_match    |
|    04 Mar 13 22:08:27    |
   
   From: vyacheslav@kononenko.net   
      
   { Please limit your text to fit within 80 columns, preferably around 70,   
    so that readers don't have to scroll horizontally to read each line.   
    This article has been reformatted manually by the moderator. -mod }   
      
   I hit an issue with using boost::regex_match()   
      
   boost::regex reg( ".*" );   
   std::string function();   
   boost::smatch what;   
   if( boost::regex_match( function(), what, reg ) )   
    std::cout << what[0].str() << std::endl;   
      
   This code seems to have UB, as boost::smatch from boost-regex stores   
   positions in original string and use them for str() method. But the   
   problem is that temporary of std::string destroyed as soon as   
   boost::regex_match() terminates.   
      
   Bad thing this issue is not easy to diagnose as passing temporary of   
   std::string to const reference is completely legit, and to understand   
   that there is issue with such issue you have to dig into implementation.   
      
   I believe following code has the same problem:   
      
   boost::regex reg( ".*" );   
   boost::smatch what;   
   if( boost::regex_match( std::string( "abc" ), what, reg ) )   
    std::cout << what[0].str() << std::endl;   
      
   std::regex_match has the same signature when match is passed. Does it   
   have similar issue?   
      
   details can be found at http://stackoverflow.com/questions/15213   
   62/passing-temporary-stdstring-to-boostregex-match/15214343#15214343   
      
      
   --   
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
    [ comp.lang.c++.moderated. First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca