From eb070303080b24458d3740424aa7aa8a45fa49eb Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Fri, 19 Mar 2021 07:15:05 +0900 Subject: numeric.c: avoid integer overflow; close #5384 Since `mruby` does not have `Bignum`, `Float#divmod` could overflow, so it will return `Float` values when the divided value does not fit in `mrb_int`. This behavior will be changed when `Bignum` is introduced to `mruby` in the future. --- src/numeric.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/numeric.c b/src/numeric.c index 74e3a8b82..b931a0f35 100644 --- a/src/numeric.c +++ b/src/numeric.c @@ -1016,7 +1016,10 @@ flo_divmod(mrb_state *mrb, mrb_value x) mrb_value a, b; flodivmod(mrb, mrb_float(x), mrb_to_flo(mrb, y), &div, &mod); - a = mrb_int_value(mrb, (mrb_int)div); + if (!FIXABLE_FLOAT(div)) + a = mrb_float_value(mrb, div); + else + a = mrb_int_value(mrb, (mrb_int)div); b = mrb_float_value(mrb, mod); return mrb_assoc_new(mrb, a, b); } -- cgit v1.2.3