From 185b7e92a37548255fcc8c5ab08b72abeadaf74e Mon Sep 17 00:00:00 2001 From: Peter Zhu Date: Fri, 8 Dec 2023 15:18:07 -0500 Subject: [PATCH] Make WeakKeyMap 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_weakkeymap.rb | 4 ++++ weakmap.c | 4 +++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/test/ruby/test_weakkeymap.rb b/test/ruby/test_weakkeymap.rb index be3e80cec46932..799cee2d75b85b 100644 --- a/test/ruby/test_weakkeymap.rb +++ b/test/ruby/test_weakkeymap.rb @@ -123,6 +123,10 @@ def test_compaction end; end + def test_gc_compact_stress + EnvUtil.under_gc_compact_stress { ObjectSpace::WeakKeyMap.new } + end + private def assert_weak_include(m, k, n = 100) diff --git a/weakmap.c b/weakmap.c index 4e568e676bcc97..641e530f58a334 100644 --- a/weakmap.c +++ b/weakmap.c @@ -609,7 +609,9 @@ wkmap_compact(void *ptr) { struct weakkeymap *w = ptr; - st_foreach_with_replace(w->table, wkmap_compact_table_i, wkmap_compact_table_replace, (st_data_t)0); + if (w->table) { + st_foreach_with_replace(w->table, wkmap_compact_table_i, wkmap_compact_table_replace, (st_data_t)0); + } } static const rb_data_type_t weakkeymap_type = {