array.c: add recursion detection to Array#== and Array#eql?

Prevent SystemStackError when comparing arrays with circular references.
Uses the same recursion detection mechanism as Hash equality methods.

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Yukihiro "Matz" Matsumoto
2025-07-07 14:30:42 +09:00
parent 5a85350121
commit 38882aecee
+10
View File
@@ -1669,6 +1669,11 @@ mrb_ary_eq(mrb_state *mrb, mrb_value ary1)
if (n == 1) return mrb_true_value();
if (n == 0) return mrb_false_value();
/* Check for recursion */
if (MRB_RECURSIVE_BINARY_P(mrb, MRB_OPSYM(eq), ary1, ary2)) {
return mrb_false_value();
}
int ai = mrb_gc_arena_save(mrb);
for (mrb_int i=0; i<RARRAY_LEN(ary1); i++) {
mrb_value eq = mrb_funcall_id(mrb, mrb_ary_entry(ary1, i), MRB_OPSYM(eq), 1, mrb_ary_entry(ary2, i));
@@ -1695,6 +1700,11 @@ mrb_ary_eql(mrb_state *mrb, mrb_value ary1)
if (n == 1) return mrb_true_value();
if (n == 0) return mrb_false_value();
/* Check for recursion */
if (MRB_RECURSIVE_BINARY_P(mrb, MRB_SYM_Q(eql), ary1, ary2)) {
return mrb_false_value();
}
int ai = mrb_gc_arena_save(mrb);
for (mrb_int i=0; i<RARRAY_LEN(ary1); i++) {
mrb_value eq = mrb_funcall_id(mrb, mrb_ary_entry(ary1, i), MRB_SYM_Q(eql), 1, mrb_ary_entry(ary2, i));