How to check for signed integer overflow in C without undefined behaviour?

Integer overflow is the canonical example of “undefined behaviour” in C (noting that operations on unsigned integers never overflow, they are defined to wrap-around instead). This means that once you’ve executed x + y, if it overflowed, you’re already hosed. It’s too late to do any checking – your program could have crashed already. Think of it like checking for division by zero – if you wait until after the division has been executed to check, it’s already too late.

So this implies that method (1) is the only correct way to do it. For max, you can use INT_MAX from <limits.h>.

If x and/or y can be negative, then things are harder – you need to do the test in such a way that the test itself can’t cause overflow.

if ((y > 0 && x > INT_MAX - y) ||
    (y < 0 && x < INT_MIN - y))
{
    /* Oh no, overflow */
}
else
{
    sum = x + y;
}

Leave a Comment