From cf8df563c0ea9b98714e701ad235acbefc091558 Mon Sep 17 00:00:00 2001 From: cremno Date: Mon, 5 May 2014 15:28:26 +0200 Subject: add function for checked mrb_int subtraction --- src/vm.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) (limited to 'src/vm.c') diff --git a/src/vm.c b/src/vm.c index 85a121f2c..ae19db0b6 100644 --- a/src/vm.c +++ b/src/vm.c @@ -1669,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; } @@ -1876,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 { -- cgit v1.2.3