Issue #276 - Possible fix

Do not perform multiplication until we are sure the operation will not over/under flow.
This commit is contained in:
Justin Ethier 2018-09-09 18:42:35 -04:00
parent 21616727d1
commit b2326b89f4

View file

@ -3053,9 +3053,20 @@ static int Cyc_checked_sub(int x, int y, int *result)
// Code from http://stackoverflow.com/q/1815367/101258 // Code from http://stackoverflow.com/q/1815367/101258
static int Cyc_checked_mul(int x, int y, int *result) static int Cyc_checked_mul(int x, int y, int *result)
{ {
// *result = x * y;
// return (*result != 0 && (*result)/x != y) || // Overflow
// (*result > CYC_FIXNUM_MAX) ||
// (*result < CYC_FIXNUM_MIN);
uint xu, yu, c;
c = 1UL<<31UL;
xu = x < 0 ? -1 : x;
yu = y < 0 ? -1 : y;
if (yu != 0 && xu > (c / yu)) return 1; // Overflow
*result = x * y; *result = x * y;
return (*result != 0 && (*result)/x != y) || // Overflow
(*result > CYC_FIXNUM_MAX) || return (*result > CYC_FIXNUM_MAX) ||
(*result < CYC_FIXNUM_MIN); (*result < CYC_FIXNUM_MIN);
} }