From ef583c93ebad9eb9cf988e35bfd8ee22fbedd2c0 Mon Sep 17 00:00:00 2001 From: Luke Gruber Date: Thu, 29 Jan 2026 18:34:30 -0500 Subject: Fix NEWOBJ hook calling `rb_obj_memsize_of` on TypedData object (#16002) Fix NEWOBJ hook calling cruby functions on objects not filled yet. Objects like `TypedData` need to be zeroed out when calling `rb_obj_memsize_of`. Other object types need `fields_obj` to be 0 when they don't have one, etc. Fixes [Bug #21854] --- gc.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/gc.c b/gc.c index 935a9f5d4b..407541b309 100644 --- a/gc.c +++ b/gc.c @@ -1014,9 +1014,7 @@ newobj_of(rb_ractor_t *cr, VALUE klass, VALUE flags, shape_id_t shape_id, bool w int lev = RB_GC_VM_LOCK_NO_BARRIER(); { size_t slot_size = rb_gc_obj_slot_size(obj); - if (slot_size > RVALUE_SIZE) { - memset((char *)obj + RVALUE_SIZE, 0, slot_size - RVALUE_SIZE); - } + memset((char *)obj + sizeof(struct RBasic), 0, slot_size - sizeof(struct RBasic)); /* We must disable GC here because the callback could call xmalloc * which could potentially trigger a GC, and a lot of code is unsafe @@ -1163,17 +1161,19 @@ rb_objspace_data_type_memsize(VALUE obj) { size_t size = 0; if (RTYPEDDATA_P(obj)) { - const rb_data_type_t *type = RTYPEDDATA_TYPE(obj); const void *ptr = RTYPEDDATA_GET_DATA(obj); - if (RTYPEDDATA_EMBEDDABLE_P(obj) && !RTYPEDDATA_EMBEDDED_P(obj)) { + if (ptr) { + const rb_data_type_t *type = RTYPEDDATA_TYPE(obj); + if (RTYPEDDATA_EMBEDDABLE_P(obj) && !RTYPEDDATA_EMBEDDED_P(obj)) { #ifdef HAVE_MALLOC_USABLE_SIZE - size += malloc_usable_size((void *)ptr); + size += malloc_usable_size((void *)ptr); #endif - } + } - if (ptr && type->function.dsize) { - size += type->function.dsize(ptr); + if (type->function.dsize) { + size += type->function.dsize(ptr); + } } } -- cgit v1.2.3