numeric.c: add boundary checks in Float#to_f.

- check_num_exact() before rounding the float value
- check integer (mrb_int) overflow.
This commit is contained in:
Yukihiro "Matz" Matsumoto
2022-04-16 09:31:35 +09:00
parent 790910b50a
commit c0ef940c90
+5 -1
View File
@@ -1011,10 +1011,14 @@ flo_to_i(mrb_state *mrb, mrb_value num)
{
mrb_float f = mrb_float(num);
mrb_check_num_exact(mrb, f);
if (!FIXABLE_FLOAT(f)) {
mrb_int_overflow(mrb, "to_f");
}
if (f > 0.0) f = floor(f);
if (f < 0.0) f = ceil(f);
mrb_check_num_exact(mrb, f);
return mrb_int_value(mrb, (mrb_int)f);
}