Handle potential overflow in `int_div` and `flo_idiv`.

parent 5134031e
......@@ -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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment