From 07b9b53459d1842e7066b248d48ef4952c6868d3 Mon Sep 17 00:00:00 2001 From: Peter Zhu Date: Tue, 16 Jan 2024 10:30:31 -0500 Subject: [PRISM] Fix crash with empty ensure blocks Fixes ruby/prism#2179. --- prism_compile.c | 4 +++- test/ruby/test_compile_prism.rb | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/prism_compile.c b/prism_compile.c index 88362fa7ac..722dabf8ab 100644 --- a/prism_compile.c +++ b/prism_compile.c @@ -3247,14 +3247,16 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, PM_PUTNIL_UNLESS_POPPED; } } + ADD_LABEL(ret, eend); + ADD_LABEL(ret, econt); + if (!popped) { PM_NOP; } pm_statements_node_t *statements = begin_node->ensure_clause->statements; if (statements) { PM_COMPILE((pm_node_t *)statements); - ADD_LABEL(ret, econt); PM_POP_UNLESS_POPPED; } diff --git a/test/ruby/test_compile_prism.rb b/test/ruby/test_compile_prism.rb index cd521e6766..bc9f0b3aba 100644 --- a/test/ruby/test_compile_prism.rb +++ b/test/ruby/test_compile_prism.rb @@ -962,6 +962,22 @@ module Prism end prism_test_ensure_node CODE + + # Test empty ensure block + assert_prism_eval(<<~RUBY) + res = [] + + begin + begin + raise + ensure + end + rescue + res << "rescue" + end + + res + RUBY end def test_NextNode -- cgit v1.2.3