summaryrefslogtreecommitdiffhomepage
path: root/src/numeric.c
diff options
context:
space:
mode:
authorYukihiro Matsumoto <[email protected]>2012-06-13 19:05:33 +0900
committerYukihiro Matsumoto <[email protected]>2012-06-13 19:05:33 +0900
commit8e574a9e7433cb6f5b17a8cac8e66a54fc71a03f (patch)
treebdb490fa88052d583468e84dd679cea29fea66c4 /src/numeric.c
parent2ded555a91a63744fcc20bdd67c50ff113095303 (diff)
downloadmruby-8e574a9e7433cb6f5b17a8cac8e66a54fc71a03f.tar.gz
mruby-8e574a9e7433cb6f5b17a8cac8e66a54fc71a03f.zip
integer overflow in fixnum plus and minus
Diffstat (limited to 'src/numeric.c')
-rw-r--r--src/numeric.c6
1 files changed, 4 insertions, 2 deletions
diff --git a/src/numeric.c b/src/numeric.c
index 81933237e..746078fd9 100644
--- a/src/numeric.c
+++ b/src/numeric.c
@@ -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);