LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 34297 - regex constructor does not throw an exception for invalid backreferences in basic/grep patterns
Summary: regex constructor does not throw an exception for invalid backreferences in b...
Status: RESOLVED FIXED
Alias: None
Product: libc++
Classification: Unclassified
Component: All Bugs (show other bugs)
Version: unspecified
Hardware: PC All
: P enhancement
Assignee: Mark de Wever
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-08-23 00:12 PDT by Arthur O'Dwyer
Modified: 2020-02-22 10:08 PST (History)
3 users (show)

See Also:
Fixed By Commit(s):


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Arthur O'Dwyer 2017-08-23 00:12:24 PDT
#include <regex>
#include <stdio.h>

int main() {
    std::regex rx("(cat)\\1", std::regex::basic);
    puts("Constructed without throwing: oops!");
    try {
        std::regex_match("(cat)", rx);
    } catch (std::exception& e) {
        puts("regex_match threw instead: oops!");
        puts(e.what());
    }
}

The output is:

Constructed without throwing: oops!
regex_match threw instead: oops!
The expression contained an invalid back reference.

I claim that the constructor should detect and throw on this pattern, rather than throwing at match time. The same issue happens with the "std::regex::grep" dialect, too.
Comment 1 Mark de Wever 2019-05-25 12:04:50 PDT
I had a look at this bug and while investigating I found another bug. Posted the following patches for review.

https://reviews.llvm.org/D62451
https://reviews.llvm.org/D62452
https://reviews.llvm.org/D62453