Skip to content

Commit

Permalink
Make WeakMap safe for compaction during allocation
Browse files Browse the repository at this point in the history
During allocation, the table may not have been allocated yet which would
crash in the st_foreach.
  • Loading branch information
peterzhu2118 committed Dec 12, 2023
1 parent d313c82 commit 33cf8f6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
4 changes: 4 additions & 0 deletions test/ruby/test_weakmap.rb
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion weakmap.c
Expand Up @@ -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 = {
Expand Down

0 comments on commit 33cf8f6

Please sign in to comment.