From 5ca2d442cee1fd4cee3c1a79f7a1e0e658c13727 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Mon, 7 Jul 2025 14:34:56 +0900 Subject: [PATCH] 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 --- mrbgems/mruby-struct/src/struct.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/mrbgems/mruby-struct/src/struct.c b/mrbgems/mruby-struct/src/struct.c index 6e616b0d7..eec7dc045 100644 --- a/mrbgems/mruby-struct/src/struct.c +++ b/mrbgems/mruby-struct/src/struct.c @@ -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);