From 843b4f49ee82c572405d466f28d4305e21d6e6c2 Mon Sep 17 00:00:00 2001 From: Peter Zhu Date: Thu, 31 Oct 2024 11:31:12 -0400 Subject: [PATCH] Fix assertion when envval of proc is Qundef The following code crashes with assertions enabled because envval could be Qundef: {}.to_proc.dup --- test/ruby/test_proc.rb | 19 +++++++++++++++++++ vm_core.h | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/test/ruby/test_proc.rb b/test/ruby/test_proc.rb index 304717e43c..fcf00776a6 100644 --- a/test/ruby/test_proc.rb +++ b/test/ruby/test_proc.rb @@ -377,6 +377,7 @@ class TestProc < Test::Unit::TestCase end def test_dup_clone + # iseq backed proc b = proc {|x| x + "bar" } class << b; attr_accessor :foo; end @@ -389,6 +390,24 @@ class TestProc < Test::Unit::TestCase assert_equal("foobar", bc.call("foo")) bc.foo = :foo assert_equal(:foo, bc.foo) + + # ifunc backed proc + b = {foo: "bar"}.to_proc + + bd = b.dup + assert_equal("bar", bd.call(:foo)) + + bc = b.clone + assert_equal("bar", bc.call(:foo)) + + # symbol backed proc + b = :to_s.to_proc + + bd = b.dup + assert_equal("testing", bd.call(:testing)) + + bc = b.clone + assert_equal("testing", bc.call(:testing)) end def test_dup_subclass diff --git a/vm_core.h b/vm_core.h index 9aa93b7e21..e5169685f1 100644 --- a/vm_core.h +++ b/vm_core.h @@ -1493,7 +1493,7 @@ VM_ENV_ESCAPED_P(const VALUE *ep) static inline int vm_assert_env(VALUE obj) { - VM_ASSERT(imemo_type_p(obj, imemo_env)); + VM_ASSERT(obj == Qundef || imemo_type_p(obj, imemo_env)); return 1; } #endif