Skip to content

<regex>: Make back-references to unmatched capture groups not match anything in POSIX basic regexes #5376

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all 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 @@ -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;
Expand All @@ -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;
}
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 @@ -1440,6 +1440,23 @@ void test_gh_5371() {
g_regexTester.should_match("", R"(\B)");
}

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 @@ -1481,6 +1498,7 @@ int main() {
test_gh_5362();
test_gh_5364();
test_gh_5371();
test_gh_5374();

return g_regexTester.result();
}