gc.c: check if the struct has fields before marking the fields_obj

If GC trigger in the middle of `struct_alloc`, and the struct has more
than 3 elements, then `fields_obj` reference is garbage.

We must first check the shape to know if it was actually initialized.
This commit is contained in:
Jean Boussier 2025-12-03 20:40:22 +01:00
parent 228d13f6ed
commit 8d1a6bc48b
Notes: git 2025-12-03 20:15:43 +00:00

2
gc.c
View File

@ -3303,7 +3303,7 @@ rb_gc_mark_children(void *objspace, VALUE obj)
gc_mark_internal(ptr[i]);
}
if (!FL_TEST_RAW(obj, RSTRUCT_GEN_FIELDS)) {
if (rb_shape_obj_has_fields(obj) && !FL_TEST_RAW(obj, RSTRUCT_GEN_FIELDS)) {
gc_mark_internal(RSTRUCT_FIELDS_OBJ(obj));
}