diff options
| author | Peter Zhu <peter@peterzhu.ca> | 2023-06-27 14:34:03 -0400 |
|---|---|---|
| committer | Peter Zhu <peter@peterzhu.ca> | 2023-06-28 09:50:51 -0400 |
| commit | a500eb9f8c487c9245d82f9b3c64cd3ddf44c836 (patch) | |
| tree | 6be42567aa793b3d046e8217a54f1aa74d4c6114 | |
| parent | 3d7a6bbc12d9eed9f1e0135c44fdeab08434e4b5 (diff) | |
Fix memory leak in Ripper
The following script leaks memory in Ripper:
```ruby
require "ripper"
20.times do
100_000.times do
Ripper.parse("")
end
puts `ps -o rss= -p #{$$}`
end
```
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/7985
| -rw-r--r-- | ext/ripper/ripper_init.c.tmpl | 1 | ||||
| -rw-r--r-- | test/ripper/test_ripper.rb | 8 |
2 files changed, 9 insertions, 0 deletions
diff --git a/ext/ripper/ripper_init.c.tmpl b/ext/ripper/ripper_init.c.tmpl index db58d64fe6..32b8aa4398 100644 --- a/ext/ripper/ripper_init.c.tmpl +++ b/ext/ripper/ripper_init.c.tmpl @@ -33,6 +33,7 @@ ripper_parser_free2(void *ptr) { struct ripper *r = (struct ripper*)ptr; ripper_parser_free(r->p); + xfree(r); } static size_t diff --git a/test/ripper/test_ripper.rb b/test/ripper/test_ripper.rb index 76276c54ef..a3fa050998 100644 --- a/test/ripper/test_ripper.rb +++ b/test/ripper/test_ripper.rb @@ -141,6 +141,14 @@ end assert_nothing_raised { Ripper.lex src } end + def test_no_memory_leak + assert_no_memory_leak(%w(-rripper), "", "#{<<~'end;'}", rss: true) + 10_000_000.times do + Ripper.parse("") + end + end; + end + class TestInput < self Input = Struct.new(:lines) do def gets |
