diff options
| author | Peter Zhu <peter@peterzhu.ca> | 2024-07-29 20:45:26 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-29 17:45:26 -0700 |
| commit | ce565cd4b851977bf37a470bee54e441bb60486d (patch) | |
| tree | 1a7fea867b14c3a8a88dc07c3fc3c29fa3ebcb4a /test/ruby/test_string.rb | |
| parent | 6d744837abc3f7f71a1f10c7ca399201f6f05e43 (diff) | |
[Bug #20653] Fix memory leak in String#start_with? when regexp times out (#11255)
Fix memory leak in String#start_with? when regexp times out
[Bug #20653]
This commit refactors how Onigmo handles timeout. Instead of raising a
timeout error, onig_search will return a ONIGERR_TIMEOUT which the
caller can free memory, and then raise a timeout error.
This fixes a memory leak in String#start_with when the regexp times out.
For example:
regex = Regexp.new("^#{"(a*)" * 10_000}x$", timeout: 0.000001)
str = "a" * 1000000 + "x"
10.times do
100.times do
str.start_with?(regex)
rescue
end
puts `ps -o rss= -p #{$$}`
end
Before:
33216
51936
71152
81728
97152
103248
120384
133392
133520
133616
After:
14912
15376
15824
15824
16128
16128
16144
16144
16160
16160
Diffstat (limited to 'test/ruby/test_string.rb')
| -rw-r--r-- | test/ruby/test_string.rb | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb index 149c128ed4..dcb81cfc6d 100644 --- a/test/ruby/test_string.rb +++ b/test/ruby/test_string.rb @@ -2010,6 +2010,22 @@ CODE assert_nil($&) end + def test_start_with_timeout_memory_leak + assert_no_memory_leak([], "#{<<~"begin;"}", "#{<<~'end;'}", "[Bug #20653]", rss: true) + regex = Regexp.new("^#{"(a*)" * 10_000}x$", timeout: 0.000001) + str = "a" * 1_000_000 + "x" + + code = proc do + str.start_with?(regex) + rescue + end + + 10.times(&code) + begin; + 1_000.times(&code) + end; + end + def test_strip assert_equal(S("x"), S(" x ").strip) assert_equal(S("x"), S(" \n\r\t x \t\r\n\n ").strip) |
