Signed integer overflow is like division by zero - it leads to undefined behaviour, so you have to check if it would occur before executing the potentially-overflowing operation. Once you've overflowed, all bets are off - your code could do anything.
The *_MAX
and _MIN
macros defined in <limits.h>
come in handy for this, but you need to be careful not to invoke undefined behaviour in the tests themselves. For example, to check if a * b
will overflow given int a, b;
, you can use:
if ((b > 0 && a <= INT_MAX / b && a >= INT_MIN / b) ||
(b == 0) ||
(b == -1 && a >= -INT_MAX) ||
(b < -1 && a >= INT_MAX / b && a <= INT_MIN / b))
{
result = a * b;
}
else
{
/* calculation would overflow */
}
(Note that one subtle pitfall this avoids is that you can't calculate INT_MIN / -1
- such a number isn't guaranteed to be representable and indeed causes a fatal trap on common platforms).