From 33cf8f640bab35c463186ef0856689c22559fbeb Mon Sep 17 00:00:00 2001 From: Peter Zhu Date: Fri, 8 Dec 2023 15:17:51 -0500 Subject: [PATCH] Make WeakMap safe for compaction during allocation During allocation, the table may not have been allocated yet which would crash in the st_foreach. --- test/ruby/test_weakmap.rb | 4 ++++ weakmap.c | 4 +++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/test/ruby/test_weakmap.rb b/test/ruby/test_weakmap.rb index a30004bce3be1c..0371afa77a03de 100644 --- a/test/ruby/test_weakmap.rb +++ b/test/ruby/test_weakmap.rb @@ -237,6 +237,10 @@ def test_compaction end; end + def test_gc_compact_stress + EnvUtil.under_gc_compact_stress { ObjectSpace::WeakMap.new } + end + def test_replaced_values_bug_19531 a = "A".dup b = "B".dup diff --git a/weakmap.c b/weakmap.c index 6d66181ffc53b2..4e568e676bcc97 100644 --- a/weakmap.c +++ b/weakmap.c @@ -144,7 +144,9 @@ wmap_compact(void *ptr) { struct weakmap *w = ptr; - st_foreach(w->table, wmap_compact_table_i, (st_data_t)w->table); + if (w->table) { + st_foreach(w->table, wmap_compact_table_i, (st_data_t)w->table); + } } static const rb_data_type_t weakmap_type = {