From 3324773f5696b322749fe9a02a5dc6099d4d2a72 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Fri, 2 Aug 2024 04:50:00 +0900 Subject: [PATCH] gc.c (mrb_gc_register): protect `obj` from GC during execution Because `mrb_ary_new()` and `mrb_ary_push()` can cause GC; fix #6317 --- src/gc.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/gc.c b/src/gc.c index 8c3b8d5b8..a54cf4a0b 100644 --- a/src/gc.c +++ b/src/gc.c @@ -418,11 +418,14 @@ mrb_gc_register(mrb_state *mrb, mrb_value obj) if (mrb_immediate_p(obj)) return; table = mrb_gv_get(mrb, GC_ROOT_SYM); + int ai = mrb_gc_arena_save(mrb); + mrb_gc_protect(mrb, obj); if (mrb_nil_p(table) || !mrb_array_p(table)) { table = mrb_ary_new(mrb); mrb_gv_set(mrb, GC_ROOT_SYM, table); } mrb_ary_push(mrb, table, obj); + mrb_gc_arena_restore(mrb, ai); } /* mrb_gc_unregister() removes the object from GC root. */