mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
mruby-array-ext: fix use-after-free in ary_compact_bang
This commit fixes a use-after-free vulnerability in `ary_compact_bang` by replacing pointer-based iteration with index-based loops. This prevents raw pointers from becoming stale after a garbage collection cycle is triggered by `mrb_ary_modify`. Co-authored-by: Gemini <gemini@google.com>
This commit is contained in:
@@ -235,10 +235,10 @@ ary_compact_bang(mrb_state *mrb, mrb_value self)
|
||||
mrb_int len = ARY_LEN(a);
|
||||
|
||||
mrb_ary_modify(mrb, a);
|
||||
mrb_value *p = ARY_PTR(a);
|
||||
/* a is still valid here, as mrb_ary_modify only modifies the RArray struct, not reallocates it */
|
||||
for (i = 0; i < len; i++) {
|
||||
if (!mrb_nil_p(p[i])) {
|
||||
if (i != j) p[j] = p[i];
|
||||
if (!mrb_nil_p(RARRAY_PTR(self)[i])) {
|
||||
if (i != j) RARRAY_PTR(self)[j] = RARRAY_PTR(self)[i];
|
||||
j++;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user