From 6ebcf25de2859b5b6402b7e8b181066c32d0e0bf Mon Sep 17 00:00:00 2001 From: Peter Zhu Date: Wed, 29 Nov 2023 10:05:19 -0500 Subject: [PATCH] GC guard catch_table_ary in iseq_set_exception_table The function iseq_set_exception_table allocates memory which can cause a GC compaction to run. Since catch_table_ary is not on the stack, it can be moved, which would make tptr incorrect. --- compile.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/compile.c b/compile.c index 7e8b6c37b8..24d980e95f 100644 --- a/compile.c +++ b/compile.c @@ -2796,9 +2796,11 @@ iseq_set_exception_table(rb_iseq_t *iseq) struct iseq_catch_table_entry *entry; ISEQ_BODY(iseq)->catch_table = NULL; - if (NIL_P(ISEQ_COMPILE_DATA(iseq)->catch_table_ary)) return COMPILE_OK; - tlen = (int)RARRAY_LEN(ISEQ_COMPILE_DATA(iseq)->catch_table_ary); - tptr = RARRAY_CONST_PTR(ISEQ_COMPILE_DATA(iseq)->catch_table_ary); + + VALUE catch_table_ary = ISEQ_COMPILE_DATA(iseq)->catch_table_ary; + if (NIL_P(catch_table_ary)) return COMPILE_OK; + tlen = (int)RARRAY_LEN(catch_table_ary); + tptr = RARRAY_CONST_PTR(catch_table_ary); if (tlen > 0) { struct iseq_catch_table *table = xmalloc(iseq_catch_table_bytes(tlen)); @@ -2834,6 +2836,8 @@ iseq_set_exception_table(rb_iseq_t *iseq) RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->catch_table_ary, 0); /* free */ } + RB_GC_GUARD(catch_table_ary); + return COMPILE_OK; }