From cbeb392d7d04a21cb69df8102ed9141b6ebf5a74 Mon Sep 17 00:00:00 2001 From: dearblue Date: Tue, 14 Jun 2022 22:31:45 +0900 Subject: [PATCH] Fixed possible inconsistency in `gc_protect()` If `mrb_realloc()` raises an out-of-memory exception, `arena_capa` will hold the wrong value. --- src/gc.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/gc.c b/src/gc.c index 0f5f21d26..4f55dc6ae 100644 --- a/src/gc.c +++ b/src/gc.c @@ -459,8 +459,9 @@ gc_protect(mrb_state *mrb, mrb_gc *gc, struct RBasic *p) #else if (gc->arena_idx >= gc->arena_capa) { /* extend arena */ - gc->arena_capa = (int)(gc->arena_capa * 3 / 2); - gc->arena = (struct RBasic**)mrb_realloc(mrb, gc->arena, sizeof(struct RBasic*)*gc->arena_capa); + int newcapa = gc->arena_capa * 3 / 2; + gc->arena = (struct RBasic**)mrb_realloc(mrb, gc->arena, sizeof(struct RBasic*)*newcapa); + gc->arena_capa = newcapa; } #endif gc->arena[gc->arena_idx++] = p;