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
This commit fixes a use-after-free vulnerability in `ary_compact` by replacing a pointer-based loop with an index-based loop. This prevents a raw pointer from becoming stale after a garbage collection cycle is triggered by `mrb_ary_push`. Co-authored-by: Gemini <gemini@google.com>
This commit is contained in:
@@ -206,11 +206,11 @@ ary_compact(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
mrb_value ary = mrb_ary_new(mrb);
|
||||
mrb_int len = RARRAY_LEN(self);
|
||||
mrb_value *p = RARRAY_PTR(self);
|
||||
|
||||
for (mrb_int i = 0; i < len; i++) {
|
||||
if (!mrb_nil_p(p[i])) {
|
||||
mrb_ary_push(mrb, ary, p[i]);
|
||||
mrb_value v = RARRAY_PTR(self)[i];
|
||||
if (!mrb_nil_p(v)) {
|
||||
mrb_ary_push(mrb, ary, v);
|
||||
}
|
||||
}
|
||||
return ary;
|
||||
|
||||
Reference in New Issue
Block a user