integer overflow in fixnum plus and minus

This commit is contained in:
Yukihiro Matsumoto
2012-06-13 19:05:33 +09:00
parent 2ded555a91
commit 8e574a9e74
+4 -2
View File
@@ -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);