mruby-numeric-ext: fix integer overflow in Integer#lcm

check for overflow using mrb_int_mul_overflow() in the LCM
computation to avoid undefined behavior when the result exceeds
mrb_int range. raises RangeError instead.

reported by OSS-Fuzz (clusterfuzz-testcase-6501272051318784).

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Yukihiro "Matz" Matsumoto
2026-02-15 22:25:38 +09:00
parent 7f6f2a85fc
commit 070bef24ab
+5 -1
View File
@@ -162,7 +162,11 @@ int_lcm(mrb_state *mrb, mrb_value x)
if (a < 0) a = -a;
if (b < 0) b = -b;
return mrb_int_value(mrb, (a / gcd_val) * b);
mrb_int lcm_val;
if (mrb_int_mul_overflow(a / gcd_val, b, &lcm_val)) {
mrb_int_overflow(mrb, "lcm");
}
return mrb_int_value(mrb, lcm_val);
}
/*