summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2020-08-27 09:43:56 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2020-10-12 18:20:05 +0900
commite479a427579e863899ed2bd3c5a90e54a7aa46e1 (patch)
treefcddcae6fd5ac2a4514b4c5bc10b494f1df8e2a8
parent5134031e189e1cdde198e1c09f7b1d22bf2a1ce0 (diff)
downloadmruby-e479a427579e863899ed2bd3c5a90e54a7aa46e1.tar.gz
mruby-e479a427579e863899ed2bd3c5a90e54a7aa46e1.zip
Handle potential overflow in `int_div` and `flo_idiv`.
-rw-r--r--src/numeric.c10
1 files changed, 8 insertions, 2 deletions
diff --git a/src/numeric.c b/src/numeric.c
index a5fb87940..f6c2c57a1 100644
--- a/src/numeric.c
+++ b/src/numeric.c
@@ -120,7 +120,10 @@ int_div(mrb_state *mrb, mrb_value xv)
mrb_get_args(mrb, "o", &yv);
if (mrb_float_p(yv)) {
- return mrb_fixnum_value((mrb_int)((mrb_float)mrb_integer(xv)/mrb_float(yv)));
+ double d = mrb_integer(xv)/mrb_float(yv);
+ if (MRB_INT_MIN <= d && d <= MRB_INT_MAX)
+ return mrb_int_value(mrb, (mrb_int)d);
+ return mrb_float_value(mrb, d);
}
else
#endif
@@ -202,7 +205,10 @@ flo_idiv(mrb_state *mrb, mrb_value x)
mrb_float y;
mrb_get_args(mrb, "f", &y);
- return mrb_int_value(mrb, (mrb_int)(mrb_to_flo(mrb, x) / y));
+ y = mrb_to_flo(mrb, x) / y;
+ if (MRB_INT_MIN <= y && y <= MRB_INT_MAX)
+ return mrb_int_value(mrb, (mrb_int)y);
+ return mrb_float_value(mrb, y);
}
static mrb_value