From afd5805224f985de3bb556a4ebca427fe088d591 Mon Sep 17 00:00:00 2001 From: dearblue Date: Thu, 19 Jun 2025 22:25:13 +0900 Subject: [PATCH] `iv_foreach()` needs to update the pointer at each loop When calling a user function, a pointer retrieved outside of a loop may be invalidated. --- src/variable.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/variable.c b/src/variable.c index c16ea5bb8..8a167debb 100644 --- a/src/variable.c +++ b/src/variable.c @@ -187,9 +187,9 @@ static void iv_foreach(mrb_state *mrb, iv_tbl *t, mrb_iv_foreach_func *func, void *p) { if (t == NULL || t->alloc == 0 || t->size == 0) return; - mrb_sym *keys = (mrb_sym*)&t->ptr[t->alloc]; - mrb_value *vals = t->ptr; for (int i = 0; i < t->size; i++) { + mrb_sym *keys = (mrb_sym*)&t->ptr[t->alloc]; + mrb_value *vals = t->ptr; if ((*func)(mrb, keys[i], vals[i], p) != 0) return; } }