Skip to content

Commit 3f29c70

Browse files
<regex>: Fix matching of loops with context-dependent empty matches and bounded number of repetitions (#5820)
Co-authored-by: Stephan T. Lavavej <[email protected]>
1 parent cf36aa9 commit 3f29c70

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

stl/inc/regex

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4091,11 +4091,15 @@ bool _Matcher3<_BidIt, _Elem, _RxTraits, _It, _Alloc>::_Match_pat(_Node_base* _N
40914091
_Frame._Loop_idx_sav = _Sav._Loop_idx;
40924092
_Frame._Loop_frame_idx_sav = _Sav._Loop_frame_idx;
40934093
_Sav._Loop_frame_idx = _Frame_idx;
4094-
if (_Progress) {
4095-
++_Sav._Loop_idx;
4096-
} else { // try only one more match after an empty match
4094+
4095+
// optimization: try only one more match after an empty match
4096+
// if there is no maximum number of reps
4097+
if (!_Progress && _Nr->_Max < 0) {
40974098
_Sav._Loop_idx = _Nr->_Min;
4099+
} else {
4100+
++_Sav._Loop_idx;
40984101
}
4102+
40994103
_STD fill(_Tgt_state._Grp_valid.begin() + static_cast<ptrdiff_t>(_Sav._Group_first),
41004104
_Tgt_state._Grp_valid.end(), false);
41014105
_Next = _Nr->_Next;

tests/std/tests/VSO_0000000_regex_use/test.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2260,6 +2260,13 @@ void test_gh_5792() {
22602260
g_regexTester.should_match("bc", "(?:(?!ab))+bc");
22612261
}
22622262

2263+
void test_gh_5797() {
2264+
// GH-5797: <regex>: Loops with bounded number of repetitions and context-dependent empty alternative are mishandled
2265+
g_regexTester.should_match("bc", "(?:b|c|(?=bc)){3}");
2266+
g_regexTester.should_match("bc", "(^|b|c){3}");
2267+
g_regexTester.should_match("bc", "(^|b|c){3}", regex_constants::extended);
2268+
}
2269+
22632270
void test_gh_5798() {
22642271
// GH-5798: <regex>: Process generic loops non-recursively.
22652272
// This extends our test coverage on non-simple loops,
@@ -2398,6 +2405,7 @@ int main() {
23982405
test_gh_5774();
23992406
test_gh_5790();
24002407
test_gh_5792();
2408+
test_gh_5797();
24012409
test_gh_5798();
24022410

24032411
return g_regexTester.result();

0 commit comments

Comments
 (0)