mruby-struct: add recursion detection to Struct#== and Struct#eql?

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

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Yukihiro "Matz" Matsumoto
2025-07-07 14:34:56 +09:00
parent 38882aecee
commit 5ca2d442ce
+12
View File
@@ -511,6 +511,12 @@ mrb_struct_equal(mrb_state *mrb, mrb_value s)
if (RSTRUCT_LEN(s) != RSTRUCT_LEN(s2)) {
return mrb_false_value();
}
/* Check for recursion */
if (MRB_RECURSIVE_BINARY_P(mrb, MRB_OPSYM(eq), s, s2)) {
return mrb_false_value();
}
mrb_value *ptr = RSTRUCT_PTR(s);
mrb_value *ptr2 = RSTRUCT_PTR(s2);
mrb_int len = RSTRUCT_LEN(s);
@@ -549,6 +555,12 @@ mrb_struct_eql(mrb_state *mrb, mrb_value s)
if (RSTRUCT_LEN(s) != RSTRUCT_LEN(s2)) {
return mrb_false_value();
}
/* Check for recursion */
if (MRB_RECURSIVE_BINARY_P(mrb, MRB_SYM_Q(eql), s, s2)) {
return mrb_false_value();
}
ptr = RSTRUCT_PTR(s);
ptr2 = RSTRUCT_PTR(s2);
len = RSTRUCT_LEN(s);