diff options
| author | Peter Zhu <peter@peterzhu.ca> | 2024-07-25 12:14:26 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-25 09:14:26 -0700 |
| commit | 7571ad42f42939d172ec9a68dfe56aac724ee2ef (patch) | |
| tree | 2be49ff80f4589335e9432ce9cb4474c88e48381 /test | |
| parent | 4667f8ec10269b0b5deca459f098abbdf3bae4ec (diff) | |
[Bug #20650] Fix memory leak in Regexp capture group when timeout (#11244)
Fix memory leak in Regexp capture group when timeout
[Bug #20650]
The capture group allocates memory that is leaked when it times out.
For example:
re = Regexp.new("^#{"(a*)" * 10_000}x$", timeout: 0.000001)
str = "a" * 1000000 + "x"
10.times do
100.times do
re =~ str
rescue Regexp::TimeoutError
end
puts `ps -o rss= -p #{$$}`
end
Before:
34688
56416
78288
100368
120784
140704
161904
183568
204320
224800
After:
16288
16288
16880
16896
16912
16928
16944
17184
17184
17200
Diffstat (limited to 'test')
| -rw-r--r-- | test/ruby/test_regexp.rb | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/test/ruby/test_regexp.rb b/test/ruby/test_regexp.rb index c8caca2891..6b9efcb555 100644 --- a/test/ruby/test_regexp.rb +++ b/test/ruby/test_regexp.rb @@ -1895,6 +1895,22 @@ class TestRegexp < Test::Unit::TestCase end; end + def test_timeout_memory_leak + assert_no_memory_leak([], "#{<<~"begin;"}", "#{<<~'end;'}", "[Bug #20650]", timeout: 100, rss: true) + regex = Regexp.new("^#{"(a*)" * 10_000}x$", timeout: 0.000001) + str = "a" * 1_000_000 + "x" + + code = proc do + regex =~ str + rescue + end + + 10.times(&code) + begin; + 1_000.times(&code) + end; + end + def test_match_cache_exponential assert_separately([], "#{<<-"begin;"}\n#{<<-'end;'}") timeout = #{ EnvUtil.apply_timeout_scale(10).inspect } |
