From 48b43af37fc3b57ff33f29cbbc8e10a8aecc0eee Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Sat, 9 Jul 2022 15:21:45 +0900 Subject: [PATCH] numeric.c (mrb_div_int_value): should support integer overflow. Especially in `MRB_INT_MIN/-1` which is bigger than MRB_INT_MAX. --- src/numeric.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/numeric.c b/src/numeric.c index aac802f11..96b0a6938 100644 --- a/src/numeric.c +++ b/src/numeric.c @@ -130,7 +130,11 @@ mrb_div_int_value(mrb_state *mrb, mrb_int x, mrb_int y) mrb_int_zerodiv(mrb); } else if(x == MRB_INT_MIN && y == -1) { +#ifdef MRB_USE_BIGINT + return mrb_bint_mul_ii(mrb, x, y); +#else mrb_int_overflow(mrb, "division"); +#endif } return mrb_int_value(mrb, mrb_div_int(x, y)); } @@ -158,13 +162,7 @@ int_div(mrb_state *mrb, mrb_value x) if (a == 0) return mrb_fixnum_value(0); if (mrb_integer_p(y)) { - mrb_int b = mrb_integer(y); -#ifdef MRB_USE_BIGINT - if(a == MRB_INT_MIN && b == -1) { - return mrb_bint_mul_ii(mrb, a, b); - } -#endif - return mrb_div_int_value(mrb, a, b); + return mrb_div_int_value(mrb, a, mrb_integer(y)); } switch (mrb_type(y)) { #ifdef MRB_USE_BIGINT