[Boost-bugs] [Boost C++ Libraries] #12797: Invalid regex recursion behavior

Subject: [Boost-bugs] [Boost C++ Libraries] #12797: Invalid regex recursion behavior
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2017-01-29 14:49:10


#12797: Invalid regex recursion behavior
-------------------------------------------------+-------------------------
 Reporter: Lucas Trzesniewski | Owner: johnmaddock
  <lucas.trzesniewski@…> | Status: new
     Type: Bugs | Component: regex
Milestone: To Be Determined | Severity: Problem
  Version: Boost 1.63.0 |
 Keywords: regex recursion |
-------------------------------------------------+-------------------------
 I have found an unexpected behavior regarding recursion in regexes (in
 Perl mode). I've reduced the issue to the following test pattern:

 {{{
 (?(DEFINE)
     (?<prefix>)
     (?<dummy>x)
 )
 (?&prefix) unused | (?&prefix) match
 }}}

 If you try to match this against `foo match bar`, the `match` word should
 be found. This works in both Perl and PCRE, see the
 [https://regex101.com/r/emKagD/1 regex101 demo here].

 * Removing or commenting out the `dummy` group causes the pattern to match
 * Making the `dummy` group empty causes the pattern to match
 * Replacing `(?&prefix)` with `(?&prefix)?` causes `Encountered an
 infinite recursion.`
 * Replacing `(?&prefix)` with `(?&prefix)?` but making the definition of
 `pattern` non-empty causes the pattern to match

 Here's the full test program:

 {{{
 #!cpp
 #include <string>
 #include <iostream>
 #include <boost/regex.hpp>

 static void test()
 {
     boost::regex re(R"regex(
         (?(DEFINE)
           (?<prefix>)
           (?<dummy>x)
         )
         (?&prefix) unused | (?&prefix) match
     )regex", boost::regex::perl | boost::regex::no_mod_s |
 boost::regex::mod_x | boost::regex::optimize);

     std::string subject("foo match bar");

     std::cout << boost::regex_replace(subject, re, "[$&]",
 boost::format_all) << std::endl;
 }

 int main(int argc, char **argv)
 {
     try
     {
         test();
     }
     catch(std::exception ex)
     {
         std::cerr << ex.what() << std::endl;
     }

     return 0;
 }
 }}}

 * Actual output is `foo match bar`
 * Expected output is `foo [match] bar`

 Tested with Boost 1.63.0 on MSVC 2015. The full pattern where the issue
 appeared can be found [http://stackoverflow.com/a/41910370/3764814 here].

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/12797>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:20 UTC