Skip to content
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

Fix [Bug #20098]: set counter value for {n,m} repetition correctly #9391

Merged
merged 1 commit into from
Dec 29, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions regexec.c
Original file line number Diff line number Diff line change
Expand Up @@ -704,11 +704,11 @@ init_cache_opcodes(const regex_t* reg, OnigCacheOpcode* cache_opcodes, long* num
OnigRepeatRange *repeat_range = &reg->repeat_range[repeat_mem];
if (repeat_range->lower < repeat_range->upper) {
INC_CACHE_OPCODES;
cache_point--;
cache_point -= lookaround_nesting != 0 ? 2 : 1;
}
cache_point -= num_cache_points_in_repeat;
int repeat_bounds = repeat_range->upper == 0x7fffffff ? 1 : repeat_range->upper - repeat_range->lower;
cache_point += num_cache_points_in_repeat * repeat_range->lower + (num_cache_points_in_repeat + 1) * repeat_bounds;
cache_point += num_cache_points_in_repeat * repeat_range->lower + (num_cache_points_in_repeat + (lookaround_nesting != 0 ? 2 : 1)) * repeat_bounds;
OnigCacheOpcode* cache_opcodes_in_repeat = cache_opcodes - 1;
while (cache_opcodes_at_repeat <= cache_opcodes_in_repeat) {
cache_opcodes_in_repeat->num_cache_points_in_outer_repeat = num_cache_points_in_repeat;
Expand Down Expand Up @@ -2542,6 +2542,7 @@ match_at(regex_t* reg, const UChar* str, const UChar* end,
MATCH_CACHE_DEBUG;\
if (msa->match_cache_buf[match_cache_point_index] & match_cache_point_mask) {\
MATCH_CACHE_DEBUG_HIT;\
if (*pbegin == OP_REPEAT_INC) stkp->u.repeat.count--;\
if (cache_opcode->lookaround_nesting == 0) goto fail;\
else if (cache_opcode->lookaround_nesting < 0) {\
if (check_extended_match_cache_point(msa->match_cache_buf, match_cache_point_index, match_cache_point_mask)) {\
Expand Down
8 changes: 8 additions & 0 deletions test/ruby/test_regexp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1987,6 +1987,14 @@ def test_bug_20083 # [Bug #20083]
end
end

def test_bug_20098 # [Bug #20098]
assert /a((.|.)|bc){,4}z/.match? 'abcbcbcbcz'
assert /a(b+?c*){4,5}z/.match? 'abbbccbbbccbcbcz'
assert /a(b+?(.|.)){2,3}z/.match? 'abbbcbbbcbbbcz'
assert /a(b*?(.|.)[bc]){2,5}z/.match? 'abcbbbcbcccbcz'
assert /^(?:.+){2,4}?b|b/.match? "aaaabaa"
end

def test_linear_time_p
assert_send [Regexp, :linear_time?, /a/]
assert_send [Regexp, :linear_time?, 'a']
Expand Down