From 1cf2fa3af50203f261341ff4c250735c9da5767b Mon Sep 17 00:00:00 2001 From: Yusuke Endoh Date: Wed, 8 Nov 2023 17:34:09 +0900 Subject: Raise an exception when Hash#compare_by_identity during its iteration --- hash.c | 3 +++ test/ruby/test_hash.rb | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/hash.c b/hash.c index 83b9e9ce66..78fbfb866c 100644 --- a/hash.c +++ b/hash.c @@ -4367,6 +4367,9 @@ rb_hash_compare_by_id(VALUE hash) if (rb_hash_compare_by_id_p(hash)) return hash; rb_hash_modify_check(hash); + if (hash_iterating_p(hash)) { + rb_raise(rb_eRuntimeError, "compare_by_identity during iteration"); + } ar_force_convert_table(hash, __FILE__, __LINE__); HASH_ASSERT(RHASH_ST_TABLE_P(hash)); diff --git a/test/ruby/test_hash.rb b/test/ruby/test_hash.rb index a01f730904..970d0938bf 100644 --- a/test/ruby/test_hash.rb +++ b/test/ruby/test_hash.rb @@ -2272,4 +2272,13 @@ class TestHashOnly < Test::Unit::TestCase end; end end + + def test_compare_by_identity_during_iteration + h = { 1 => 1 } + h.each do + assert_raise(RuntimeError, "compare_by_identity during iteration") do + h.compare_by_identity + end + end + end end -- cgit v1.2.3