From e82b36035e78ccb5e5ed4e1205048440938f8bb5 Mon Sep 17 00:00:00 2001 From: dearblue Date: Thu, 30 May 2024 21:25:30 +0900 Subject: [PATCH] Fixes local variables in `mruby-binding`. There are two issues to be fixed: - `mrb_irep` could leak if `mrb_calloc()` encountered an out-of-memory exception - `mrb_proc_merge_lvar()` allocated one extra variable name. `irep->lv` can always refer to only one less range than `irep->nlocals`. Also, when `mrb_proc_merge_lvar()` extends `irep->lv`, `mrb_realloc()` with `NULL` has the same behavior as `mrb_malloc()`. --- mrbgems/mruby-binding/src/binding.c | 2 +- src/proc.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/mrbgems/mruby-binding/src/binding.c b/mrbgems/mruby-binding/src/binding.c index c269facfc..f5a491c98 100644 --- a/mrbgems/mruby-binding/src/binding.c +++ b/mrbgems/mruby-binding/src/binding.c @@ -68,7 +68,7 @@ binding_irep_new_lvspace(mrb_state *mrb) irep->flags = MRB_ISEQ_NO_FREE; irep->iseq = iseq_dummy; irep->ilen = sizeof(iseq_dummy) / sizeof(iseq_dummy[0]); - irep->lv = (mrb_sym*)mrb_calloc(mrb, 1, sizeof(mrb_sym)); /* initial allocation for dummy */ + irep->lv = NULL; irep->nlocals = 1; irep->nregs = 1; return irep; diff --git a/src/proc.c b/src/proc.c index 24cdd5802..ad177635f 100644 --- a/src/proc.c +++ b/src/proc.c @@ -461,8 +461,8 @@ mrb_proc_merge_lvar(mrb_state *mrb, mrb_irep *irep, struct REnv *env, int num, c mrb_raise(mrb, E_RUNTIME_ERROR, "unavailable local variable names"); } - irep->lv = (mrb_sym*)mrb_realloc(mrb, (mrb_sym*)irep->lv, sizeof(mrb_sym) * (irep->nlocals + num)); - env->stack = (mrb_value*)mrb_realloc(mrb, env->stack, sizeof(mrb_value) * (irep->nlocals + 1 /* self */ + num)); + irep->lv = (mrb_sym*)mrb_realloc(mrb, (mrb_sym*)irep->lv, sizeof(mrb_sym) * (irep->nlocals - 1 /* self */ + num)); + env->stack = (mrb_value*)mrb_realloc(mrb, env->stack, sizeof(mrb_value) * (irep->nlocals + num)); mrb_sym *destlv = (mrb_sym*)irep->lv + irep->nlocals - 1 /* self */; mrb_value *destst = env->stack + irep->nlocals;