diff options
| author | cremno <[email protected]> | 2014-05-05 15:24:22 +0200 |
|---|---|---|
| committer | cremno <[email protected]> | 2014-05-05 15:24:22 +0200 |
| commit | 0f3930426515848be755c5480df354fa9e2109f4 (patch) | |
| tree | 55997d13d181d852b2522aa0b3b69831b384e352 /src | |
| parent | ff03cea79b710913af1f5decec1952c14ba67812 (diff) | |
| download | mruby-0f3930426515848be755c5480df354fa9e2109f4.tar.gz mruby-0f3930426515848be755c5480df354fa9e2109f4.zip | |
add function for checked mrb_int addition
Diffstat (limited to 'src')
| -rw-r--r-- | src/numeric.c | 4 | ||||
| -rw-r--r-- | src/vm.c | 13 |
2 files changed, 5 insertions, 12 deletions
diff --git a/src/numeric.c b/src/numeric.c index 56835edbf..8ea71ada6 100644 --- a/src/numeric.c +++ b/src/numeric.c @@ -1109,9 +1109,7 @@ mrb_fixnum_plus(mrb_state *mrb, mrb_value x, mrb_value y) if (a == 0) return y; b = mrb_fixnum(y); - c = a + b; - if (((a < 0) ^ (b < 0)) == 0 && (a < 0) != (c < 0)) { - /* integer overflow */ + if (mrb_int_add_overflow(a, b, &c)) { return mrb_float_value(mrb, (mrb_float)a + (mrb_float)b); } return mrb_fixnum_value(c); @@ -12,6 +12,7 @@ #include "mruby/class.h" #include "mruby/hash.h" #include "mruby/irep.h" +#include "mruby/numeric.h" #include "mruby/proc.h" #include "mruby/range.h" #include "mruby/string.h" @@ -1610,12 +1611,7 @@ RETRY_TRY_BLOCK: x = mrb_fixnum(regs_a[0]); y = mrb_fixnum(regs_a[1]); - z = x + y; -#ifdef MRB_WORD_BOXING - z = (z << MRB_FIXNUM_SHIFT) / (1 << MRB_FIXNUM_SHIFT); -#endif - if ((x < 0) != (z < 0) && ((x < 0) ^ (y < 0)) == 0) { - /* integer overflow */ + if (mrb_int_add_overflow(x, y, &z)) { SET_FLT_VALUE(mrb, regs_a[0], (mrb_float)x + (mrb_float)y); break; } @@ -1842,10 +1838,9 @@ RETRY_TRY_BLOCK: { mrb_int x = regs[a].attr_i; mrb_int y = GETARG_C(i); - mrb_int z = x + y; + mrb_int z; - if (((x < 0) ^ (y < 0)) == 0 && (x < 0) != (z < 0)) { - /* integer overflow */ + if (mrb_int_add_overflow(x, y, &z)) { SET_FLT_VALUE(mrb, regs[a], (mrb_float)x + (mrb_float)y); break; } |
