mirror of
https://github.com/justinethier/cyclone.git
synced 2025-05-21 14:49:17 +02:00
Issue #276 - Possible fix
Do not perform multiplication until we are sure the operation will not over/under flow.
This commit is contained in:
parent
21616727d1
commit
b2326b89f4
1 changed files with 13 additions and 2 deletions
15
runtime.c
15
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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue