summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Gruber <luke.gruber@shopify.com>2026-01-29 18:34:30 -0500
committerGitHub <noreply@github.com>2026-01-29 18:34:30 -0500
commitef583c93ebad9eb9cf988e35bfd8ee22fbedd2c0 (patch)
tree42d6a148c374da76d541574bf706152ecf65b2d6
parent5fec5456b9cd9dd7fdea18ac9c43b1cf6d4cf4cb (diff)
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]
-rw-r--r--gc.c18
1 files 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);
+ }
}
}