numeric.c (int_div): add checks for division by zero.

This commit is contained in:
Yukihiro "Matz" Matsumoto
2022-07-29 17:48:02 +09:00
parent 387f383eeb
commit 6da34fa735
4 changed files with 9 additions and 1 deletions
+3
View File
@@ -1180,6 +1180,9 @@ mrb_bint_div(mrb_state *mrb, mrb_value x, mrb_value y)
return mrb_float_value(mrb,v1*v2);
}
#endif
if (mrb_integer_p(y) && mrb_integer(y) == 0) {
mrb_int_zerodiv(mrb);
}
y = mrb_as_bint(mrb, y);
struct RBigint *b = RBIGINT(x);
struct RBigint *b2 = RBIGINT(y);
+4
View File
@@ -1,6 +1,7 @@
#include <mruby.h>
#include <mruby/class.h>
#include <mruby/numeric.h>
#include <mruby/internal.h>
#include <mruby/presym.h>
#ifdef MRB_NO_FLOAT
@@ -308,6 +309,9 @@ mrb_complex_div(mrb_state *mrb, mrb_value self, mrb_value rhs)
a = complex_ptr(mrb, self);
if (mrb_type(rhs) != MRB_TT_COMPLEX) {
if (mrb_integer_p(rhs) && mrb_integer(rhs) == 0) {
mrb_int_zerodiv(mrb);
}
mrb_float f = mrb_as_float(mrb, rhs);
return complex_new(mrb, mrb_div_float(a->real, f), mrb_div_float(a->imaginary, f));
}
+2
View File
@@ -1,6 +1,7 @@
#include <mruby.h>
#include <mruby/class.h>
#include <mruby/numeric.h>
#include <mruby/internal.h>
#include <mruby/presym.h>
#ifndef MRB_NO_FLOAT
@@ -647,6 +648,7 @@ mrb_rational_div(mrb_state *mrb, mrb_value x, mrb_value y)
case MRB_TT_INTEGER:
{
mrb_int z = mrb_integer(y);
if (z == 0) mrb_int_zerodiv(mrb);
if (mrb_int_mul_overflow(p1->denominator, z, &z)) rat_overflow(mrb);
return rational_new_i(mrb, p1->numerator, z);
}
-1
View File
@@ -160,7 +160,6 @@ int_div(mrb_state *mrb, mrb_value x)
#endif
mrb_int a = mrb_integer(x);
if (a == 0) return mrb_fixnum_value(0);
if (mrb_integer_p(y)) {
return mrb_div_int_value(mrb, a, mrb_integer(y));
}