diff --git a/stl/inc/regex b/stl/inc/regex index cee712db56..a5795f344d 100644 --- a/stl/inc/regex +++ b/stl/inc/regex @@ -3662,6 +3662,9 @@ bool _Matcher<_BidIt, _Elem, _RxTraits, _It>::_Match_pat(_Node_base* _Nx) { // c case _N_back: { // check back reference + _STL_INTERNAL_CHECK( + (_Sflags & (regex_constants::extended | regex_constants::egrep | regex_constants::awk)) + == 0); // these grammars don't have backreferences _Node_back* _Node = static_cast<_Node_back*>(_Nx); if (_Tgt_state._Grp_valid[_Node->_Idx]) { // check for match _It _Res0 = _Tgt_state._Cur; @@ -3673,6 +3676,8 @@ bool _Matcher<_BidIt, _Elem, _RxTraits, _It>::_Match_pat(_Node_base* _Nx) { // c } else { _Tgt_state._Cur = _Res0; } + } else if (_Sflags & (regex_constants::basic | regex_constants::grep)) { + _Failed = true; } break; } diff --git a/tests/std/tests/VSO_0000000_regex_use/test.cpp b/tests/std/tests/VSO_0000000_regex_use/test.cpp index 78c4738059..4aa304947f 100644 --- a/tests/std/tests/VSO_0000000_regex_use/test.cpp +++ b/tests/std/tests/VSO_0000000_regex_use/test.cpp @@ -1440,6 +1440,23 @@ void test_gh_5371() { g_regexTester.should_match("", R"(\B)"); } +void test_gh_5374() { + // GH-5374: : Back-references to unmatched capture groups + // should not match in POSIX basic regular expressions + for (syntax_option_type option : {basic, grep}) { + g_regexTester.should_not_match("", R"(\(.\)*\1)", option); + g_regexTester.should_match("", R"(\(.*\)\1)", option); + g_regexTester.should_not_match("bc", R"(\(a\)*b\1c)", option); + g_regexTester.should_match("bc", R"(\(a*\)b\1c)", option); + } + + // ECMAScript's behavior is different: + g_regexTester.should_match("", R"((.)*\1)", ECMAScript); + g_regexTester.should_match("", R"((.*)\1)", ECMAScript); + g_regexTester.should_match("bc", R"((a)*b\1c)", ECMAScript); + g_regexTester.should_match("bc", R"((a*)b\1c)", ECMAScript); +} + int main() { test_dev10_449367_case_insensitivity_should_work(); test_dev11_462743_regex_collate_should_not_disable_regex_icase(); @@ -1481,6 +1498,7 @@ int main() { test_gh_5362(); test_gh_5364(); test_gh_5371(); + test_gh_5374(); return g_regexTester.result(); }