Avoid integer overflow in `rational_new`.

parent 79af3f30
...@@ -77,6 +77,9 @@ rational_new(mrb_state *mrb, mrb_int numerator, mrb_int denominator) ...@@ -77,6 +77,9 @@ rational_new(mrb_state *mrb, mrb_int numerator, mrb_int denominator)
struct mrb_rational *p; struct mrb_rational *p;
struct RBasic *rat = rational_alloc(mrb, c, &p); struct RBasic *rat = rational_alloc(mrb, c, &p);
if (denominator < 0) { if (denominator < 0) {
if (numerator == MRB_INT_MIN || denominator == MRB_INT_MIN) {
mrb_raise(mrb, E_RANGE_ERROR, "integer overflow in rational");
}
numerator *= -1; numerator *= -1;
denominator *= -1; denominator *= -1;
} }
...@@ -115,6 +118,9 @@ rational_new_f(mrb_state *mrb, mrb_float f0) ...@@ -115,6 +118,9 @@ rational_new_f(mrb_state *mrb, mrb_float f0)
if (f < 0) { neg = 1; f = -f; } if (f < 0) { neg = 1; f = -f; }
while (f != floor(f)) { n <<= 1; f *= 2; } while (f != floor(f)) { n <<= 1; f *= 2; }
if (!TYPED_FIXABLE(f, rat_float)) {
mrb_raise(mrb, E_RANGE_ERROR, "integer overflow in rational");
}
d = (mrb_int)f; d = (mrb_int)f;
/* continued fraction and check denominator each step */ /* continued fraction and check denominator each step */
......
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