From c0ef940c9060fed770cbe8388e601f911d393d49 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Sat, 16 Apr 2022 09:31:35 +0900 Subject: [PATCH] numeric.c: add boundary checks in `Float#to_f`. - check_num_exact() before rounding the float value - check integer (mrb_int) overflow. --- src/numeric.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/numeric.c b/src/numeric.c index aa6550d7a..e33f1bed7 100644 --- a/src/numeric.c +++ b/src/numeric.c @@ -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); }