[Bug #21004] Fix memory leak with "it" in parse.y

Parsing `-> do it end` in parse.y leaks memory. We can see this in the
Valgrind output:

    56 bytes in 1 blocks are definitely lost in loss record 1 of 6
        at 0x484E0DC: calloc (vg_replace_malloc.c:1675)
        by 0x188970: calloc1 (default.c:1472)
        by 0x188970: rb_gc_impl_calloc (default.c:8208)
        by 0x188970: ruby_xcalloc_body (gc.c:4598)
        by 0x18B8BC: ruby_xcalloc (gc.c:4592)
        by 0x21DCCA70: new_locations_lambda_body (ripper.y:12844)
        by 0x21DCCA70: ripper_yyparse (ripper.y:5194)
        by 0x21DDA521: rb_ruby_ripper_parse0 (ripper.y:15798)
This commit is contained in:
Peter Zhu 2025-01-04 17:02:24 -05:00 committed by Yudai Takada
parent 78d7dde21c
commit dfc9e978fb
Notes: git 2025-01-05 00:06:30 +00:00
2 changed files with 8 additions and 0 deletions

View File

@ -5155,6 +5155,7 @@ lambda : tLAMBDA[lpar]
nd_set_line(RNODE_LAMBDA($$)->nd_body, @body.end_pos.lineno);
nd_set_line($$, @args.end_pos.lineno);
nd_set_first_loc($$, @1.beg_pos);
xfree($body);
}
/*% ripper: lambda!($:args, $:body) %*/
numparam_pop(p, $numparam);

View File

@ -187,6 +187,13 @@ end
Ripper.parse("-> {")
end
end;
# [Bug #21004]
assert_no_memory_leak(%w(-rripper), "", <<~RUBY, rss: true)
1_000_000.times do
Ripper.parse("-> do it end")
end
RUBY
end
def test_sexp_no_memory_leak