From dfc9e978fba886d9a9175534551b1fc7b4d22abd Mon Sep 17 00:00:00 2001 From: Peter Zhu Date: Sat, 4 Jan 2025 17:02:24 -0500 Subject: [PATCH] [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) --- parse.y | 1 + test/ripper/test_ripper.rb | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/parse.y b/parse.y index ced7a8c86d..150fe795f8 100644 --- a/parse.y +++ b/parse.y @@ -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); diff --git a/test/ripper/test_ripper.rb b/test/ripper/test_ripper.rb index 070023b536..2b3421d827 100644 --- a/test/ripper/test_ripper.rb +++ b/test/ripper/test_ripper.rb @@ -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