From b2326b89f4553fc7355e742b6da8aa14d726ee4e Mon Sep 17 00:00:00 2001 From: Justin Ethier Date: Sun, 9 Sep 2018 18:42:35 -0400 Subject: [PATCH] Issue #276 - Possible fix Do not perform multiplication until we are sure the operation will not over/under flow. --- runtime.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/runtime.c b/runtime.c index d43aab0b..ae98b2ac 100644 --- a/runtime.c +++ b/runtime.c @@ -3053,9 +3053,20 @@ static int Cyc_checked_sub(int x, int y, int *result) // Code from http://stackoverflow.com/q/1815367/101258 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; - return (*result != 0 && (*result)/x != y) || // Overflow - (*result > CYC_FIXNUM_MAX) || + + return (*result > CYC_FIXNUM_MAX) || (*result < CYC_FIXNUM_MIN); }