Commit 8e574a9e authored by Yukihiro Matsumoto's avatar Yukihiro Matsumoto

integer overflow in fixnum plus and minus

parent 2ded555a
......@@ -1073,7 +1073,8 @@ mrb_fixnum_plus(mrb_state *mrb, mrb_value x, mrb_value y)
b = mrb_fixnum(y);
c = a + b;
if (c - b != a) {
if (((a < 0) ^ (b < 0)) == 0 && (a < 0) != (c < 0)) {
/* integer overflow */
return mrb_float_value((mrb_float)a + (mrb_float)b);
}
return mrb_fixnum_value(c);
......@@ -1111,7 +1112,8 @@ mrb_fixnum_minus(mrb_state *mrb, mrb_value x, mrb_value y)
b = mrb_fixnum(y);
c = a - b;
if (c + b != a) {
if (((a < 0) ^ (b < 0)) != 0 && (a < 0) != (c < 0)) {
/* integer overflow */
return mrb_float_value((mrb_float)a - (mrb_float)b);
}
return mrb_fixnum_value(c);
......
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