Skip to content

Commit

Permalink
Fix memory leak in Ripper
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
peterzhu2118 committed Jun 28, 2023
1 parent 3d7a6bb commit a500eb9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions ext/ripper/ripper_init.c.tmpl
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions test/ripper/test_ripper.rb
Expand Up @@ -141,6 +141,14 @@ def test_dedent_string
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
Expand Down

0 comments on commit a500eb9

Please sign in to comment.