diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2014-05-05 22:54:55 +0900 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2014-05-05 22:54:55 +0900 |
| commit | 260d38d5ee5228bcfae1a834357ed58bbb96ea80 (patch) | |
| tree | 12422d1ade4d3db453d84578e04c6db12a0a1e63 /src/vm.c | |
| parent | 2ceb71f97021d6d42b802e7edf522dd16357a932 (diff) | |
| parent | cf8df563c0ea9b98714e701ad235acbefc091558 (diff) | |
| download | mruby-260d38d5ee5228bcfae1a834357ed58bbb96ea80.tar.gz mruby-260d38d5ee5228bcfae1a834357ed58bbb96ea80.zip | |
Merge pull request #2200 from cremno/add-functions-for-safe-addition-and-subtraction
Add functions for safe addition and subtraction
Diffstat (limited to 'src/vm.c')
| -rw-r--r-- | src/vm.c | 25 |
1 files changed, 7 insertions, 18 deletions
@@ -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; } @@ -1673,12 +1669,7 @@ RETRY_TRY_BLOCK: x = mrb_fixnum(regs[a]); 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) ^ (y < 0)) != 0 && (x < 0) != (z < 0)) { - /* integer overflow */ + if (mrb_int_sub_overflow(x, y, &z)) { SET_FLT_VALUE(mrb, regs[a], (mrb_float)x - (mrb_float)y); break; } @@ -1842,10 +1833,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; } @@ -1881,10 +1871,9 @@ RETRY_TRY_BLOCK: { mrb_int x = regs_a[0].attr_i; mrb_int y = GETARG_C(i); - mrb_int z = x - y; + mrb_int z; - if ((x < 0) != (z < 0) && ((x < 0) ^ (y < 0)) != 0) { - /* integer overflow */ + if (mrb_int_sub_overflow(x, y, &z)) { SET_FLT_VALUE(mrb, regs_a[0], (mrb_float)x - (mrb_float)y); } else { |
