catching potential overflow in fixnum+fixnum cases in sexp_add similar to last change

This commit is contained in:
Alex Shinn 2011-11-28 22:20:24 +09:00
parent d9b57ab6b1
commit f632037344

View file

@ -921,6 +921,7 @@ static int sexp_number_type (sexp a) {
} }
sexp sexp_add (sexp ctx, sexp a, sexp b) { sexp sexp_add (sexp ctx, sexp a, sexp b) {
sexp_sint_t sum;
int at=sexp_number_type(a), bt=sexp_number_type(b), t; int at=sexp_number_type(a), bt=sexp_number_type(b), t;
sexp r=SEXP_VOID; sexp r=SEXP_VOID;
sexp_gc_var1(tmp); sexp_gc_var1(tmp);
@ -938,7 +939,11 @@ sexp sexp_add (sexp ctx, sexp a, sexp b) {
r = sexp_type_exception(ctx, NULL, SEXP_NUMBER, a); r = sexp_type_exception(ctx, NULL, SEXP_NUMBER, a);
break; break;
case SEXP_NUM_FIX_FIX: case SEXP_NUM_FIX_FIX:
r = sexp_fx_add(a, b); /* VM catches this case */ sum = sexp_unbox_fixnum(a) + sexp_unbox_fixnum(b);
if ((sum < SEXP_MIN_FIXNUM) || (sum > SEXP_MAX_FIXNUM))
r = sexp_add(ctx, tmp=sexp_fixnum_to_bignum(ctx, a), b);
else
r = sexp_make_fixnum(sum);
break; break;
case SEXP_NUM_FIX_FLO: case SEXP_NUM_FIX_FLO:
r = sexp_make_flonum(ctx, sexp_fixnum_to_double(a)+sexp_flonum_value(b)); r = sexp_make_flonum(ctx, sexp_fixnum_to_double(a)+sexp_flonum_value(b));