Joy of Programming: How to Detect Integer Overflow

Integer overflows often result in nasty bugs. In this column, we’ll look at some techniques to detect an overflow before it occurs.

Integer overflow happens because computers use fixed width to represent integers. So which are the operations that result in overflow? Bitwise and logical operations cannot overflow, while cast and arithmetic operations can. For example, ++ and += operators can overflow, whereas && or & operators (or even << and >> operators) cannot.

Regarding arithmetic operators, it is obvious that operations like addition, subtraction and multiplication can overflow.

How about operations like (unary) negation, division and mod (remainder)? For unary negation, -MIN_INT is equal to MIN_INT (and not MAX_INT), so it overflows. Following the same logic, division overflows for the expression (MIN_INT / -1). How about a mod operation? It does not overflow. The only possible overflow case (MIN_INT % -1) is equal to 0 (verify this yourself—the formula for % operator is a % b = a - ((a / b) * b)).

Let us focus on addition. For the statement int k = (i + j);:

If i and j are of different signs, it cannot overflow.

If i and j are of same signs (- or +), it can overflow.

If i and j are positive integers, then their sign bit is zero. If k is negative, it means its sign bit is 1—it indicates the value of (i + j) is too large to represent in k, so it overflows.

If i and j are negative integers, then their sign bit is one. If k is positive, it means its sign bit is 0—it indicates that the value of (i + j) is too small to represent in k, so it overflows.

To check for overflow, we have to provide checks for conditions 3 and 4. Here is the straightforward conversion of these two statements into code. The function isSafeToAdd returns true or false after checking for overflow.

Why? Because (i + j) overflows, and when its result is stored, it can never be greater than INT_MAX or less than INT_MIN! That’s precisely the condition (overflow) we want to detect, so it won’t work.

How about modifying the checking expression? Instead of ((i + j) > INT_MAX), we can check the condition (i > INT_MAX - j) by moving j to the RHS of the expression. So, the condition in isSafeToAdd can be rewritten as:

if( (i > INT_MAX - j) || (i < INT_MIN - j) )
return 0;

That works! But can we simplify it further? From condition 2, we know that for an overflow to occur, the signs of i and j should be different the same. If you notice the conditions in 3 and 4, the sign bit of the result (k) is different from (i and j). Does this strike you as the check that the ^ operator can be used? How about this check:

int k = (i + j);
if( ((i ^ k) & (j ^ k)) < 0)
return 0;

Let us check it. Assume that i and j are positive values and when it overflows, the result k will be negative. Now the condition (i ^ k) will be a negative value—the sign bit of i is 0 and the sign bit of k is 1; so ^ of the sign bit will be 1 and hence the value of the expression (i ^ k) is negative. So is the case for (j ^ k) and when the & of two values is negative; hence, the condition check with < 0 becomes true when there is overflow. When i and j are negative and k is positive, the condition again is < 0 (following the same logic described above).

So, yes, this also works! Though the if condition is not very easy to understand, it is correct and is also an efficient solution!

In September 1752, Great Britain switched from the Julian Calendar to the Gregorian Calendar. In order to achieve the change, 11 days were ‘omitted’ from the calendar – i.e. the day after 02 September 1752 was 14 September 1752.

On the advice of his astronomer and mathematician, Julius Caesar established a calendar in 45 B.C. This calendar was known as the Old Style or the Julian calendar. In this system, three years had 365 days each followed by a year having 366 days. Also, according to this calendar, the first day of the year was March 25 and the last day was March 24.

In 582, Pope Gregory XIII instituted a New Style calendar which is also known as the Gregorian calendar. In the Gregorian calendar 04 October 1582 was followed by 15 October 1582. Not many countries accepted this change. Later when some countries did accept this change, eleven days were dropped from the month of September 1752. The calendar system which we presently follow is known as the Gregorian Calendar.

II. If you wish to find the file(s) which contains a particular word or a phrase, then you can use the command kfind at the $ prompt.

III. You have been using many editors like vi, vim etc. Try using kwrite. It is a very user-friendly editor.