https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78276

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to James K. Lowden from comment #3)
> Here is a nonpathological example taken from a real-world problem were
> std::regex_search fails.  

This can be reduced to:

#include <regex>
#include <string>

int main()
{
  std::string pattern =
    /*1,2*/      "([[:space:]]+(LEADING|TRAILING))?"
                 "[[:space:]]+" // leading space between pairs
    /* 3   */    "("
    /* 4,5 */            "\"("  "([\"]{2}|[^\"])*" ")\""
    /* 6,7 */        "|" "'("   "([']{2}|[^'])*"   ")[']"
    /* 8,9 */        "|" "("    "[[:alnum:]]+([_-]+[[:alnum:]]+)*" ")"
    /*10,11*/        "|" "==(" "(=?[^=]+)+" ")=="
    /*     */    ")"
    /*     */    "[[:space:]]+BY[[:space:]]+"
    /* 12  */    "("
    /*13,14*/          "(\""  "([\"]{2}|[^\"])*" "\")"
    /*15,16*/      "|" "('"   "([']{2}|[^'])*"   "')"
    /*17,18*/      "|" "("    "[[:alnum:]]+([_-]+[[:alnum:]]+)*" ")"
    /*19,20*/      "|" "==("  "(=?[^=]+)*" ")=="
    /*     */    ")"
    /* 21  */    "([[:space:]]*[.])?"
    ;

  std::regex re(pattern);

  std::string input = R"(
     COPY                                                    KP003
             REPLACING ==+00001== BY  +2                          
                       == 1 ==    BY  -3 .                        
 PST-TEST-003-1.                                                  
     MOVE    01 TO REC-CT.                                        
     IF      WRK-DS-05V00-O005-001 IN GRP-003 (3) EQUAL TO +00009 
             PERFORM PASS                                         
             ELSE                                                 
             MOVE   +009 TO CORRECT-18V0                          
             MOVE   WRK-DS-05V00-O005-001  IN                     
                    GRP-003 (3) TO COMPUTED-18V0                  
             PERFORM FAIL.                                        
     PERFORM PRINT-DETAIL.                                        
     ADD     +01 TO REC-CT.                                       
     IF      WRK-DS-09V00-901 EQUAL TO +000000007                 
             PERFORM PASS                                         
             ELSE                                                 
             PERFORM FAIL                                         
             MOVE  +7 TO CORRECT-18V0                             
             MOVE  WRK-DS-09V00-901 TO COMPUTED-18V0.             
     PERFORM PRINT-DETAIL.                                        
     ADD     +01 TO REC-CT.                                       
     IF      WRK-DS-05V00-O005-001 OF GRP-002 (3) EQUAL TO +8     
             PERFORM PASS                                         
             ELSE                                                 
             MOVE +8 TO CORRECT-18V0                              
             PERFORM FAIL                                         
             MOVE WRK-DS-05V00-O005-001 IN GRP-002 (3) TO         
             COMPUTED-18V0.                                       
     PERFORM PRINT-DETAIL.                                        
     MOVE 0 TO WRK-DS-09V00-901.                                  
)";
  regex_search(input, re);
}


Clang and libc++ give up by throwing std::regex_error(error_complexity):

libc++abi: terminating due to uncaught exception of type std::__1::regex_error:
The complexity of an attempted match against a regular expression exceeded a
pre-set level.
Aborted (core dumped)

So they obviously decided it's better to do that than wait for arbitrarily
complex matches.

Reply via email to