Skip to content

Commit f4258aa

Browse files
authored
[Bug #20886] Avoid double-free in regex timeout after stack_double (ruby#12063)
Fix regex timeout double-free after stack_double As of 1057485, it's possible to crash on a double free due to `stk_alloc` AKA `msa->stack_p` being freed twice, once at the end of match_at and a second time in `FREE_MATCH_ARG` in the parent caller. Fixes [Bug #20886]
1 parent a51a6bf commit f4258aa

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

regexec.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4217,9 +4217,8 @@ match_at(regex_t* reg, const UChar* str, const UChar* end,
42174217
return ONIGERR_UNEXPECTED_BYTECODE;
42184218

42194219
timeout:
4220+
STACK_SAVE;
42204221
xfree(xmalloc_base);
4221-
if (stk_base != stk_alloc || IS_NOT_NULL(msa->stack_p))
4222-
xfree(stk_base);
42234222
return ONIGERR_TIMEOUT;
42244223
}
42254224

test/ruby/test_regexp.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1838,6 +1838,13 @@ def test_bug_20453
18381838
end;
18391839
end
18401840

1841+
def test_bug_20886
1842+
re = Regexp.new("d()*+|a*a*bc", timeout: 0.02)
1843+
assert_raise(Regexp::TimeoutError) do
1844+
re === "b" + "a" * 1000
1845+
end
1846+
end
1847+
18411848
def per_instance_redos_test(global_timeout, per_instance_timeout, expected_timeout)
18421849
assert_separately([], "#{<<-"begin;"}\n#{<<-'end;'}")
18431850
global_timeout = #{ EnvUtil.apply_timeout_scale(global_timeout).inspect }

0 commit comments

Comments
 (0)