Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions stl/inc/regex
Original file line number Diff line number Diff line change
Expand Up @@ -3643,6 +3643,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;
Expand All @@ -3654,6 +3657,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;
}
Expand Down
18 changes: 18 additions & 0 deletions tests/std/tests/VSO_0000000_regex_use/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1171,6 +1171,23 @@ void test_gh_5253() {
g_regexTester.should_not_match("a", "()*");
}

void test_gh_5374() {
// GH-5374: <regex>: 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();
Expand Down Expand Up @@ -1208,6 +1225,7 @@ int main() {
test_gh_5192();
test_gh_5214();
test_gh_5253();
test_gh_5374();

return g_regexTester.result();
}