array.c (mrb_ary_delete): protect return value; fix #6339

The C local variable is not protected from GC, so we use the function
mrb_gc_protect() to keep the value. We also keep the arena position by
mrb_gc_arena_save(), then restoring the position for every new return
value, to minimize arena size.

Small cosmetic changes (pre-increment to post-increment) are also made
in this commit.
This commit is contained in:
Yukihiro "Matz" Matsumoto
2024-09-09 14:57:32 +09:00
parent 1c4514964e
commit 0972c84773
+5 -2
View File
@@ -1557,12 +1557,15 @@ mrb_ary_delete(mrb_state *mrb, mrb_value self)
mrb_value ret = obj;
int ai = mrb_gc_arena_save(mrb);
size_t i = 0;
size_t j = 0;
for (; i < len; ++i) {
for (; i < len; i++) {
mrb_value elem = val_ptr[i];
if (mrb_equal(mrb, elem, obj)) {
mrb_gc_arena_restore(mrb, ai);
mrb_gc_protect(mrb, elem);
ret = elem;
continue;
}
@@ -1576,7 +1579,7 @@ mrb_ary_delete(mrb_state *mrb, mrb_value self)
val_ptr[j] = elem;
}
++j;
j++;
}
if (i == j) {