mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
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:
@@ -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);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user