INF: Overflow in Integer Math Expressions Not Checked

ID Number: Q38733

4.00 5.00 5.10 6.00 6.00a 6.00ax 7.00 | 5.10 6.00 6.00a

MS-DOS | OS/2

Summary:

In Microsoft C versions 5.1, 6.0, 6.0a, 6.0ax, and C/C++ version 7.0,

the operations performed by the integer arithmetic and shift operators

do not check for overflow or underflow conditions. Information may be

lost if the result of an operation cannot be represented in the type of

the operands after conversion. All expressions are evaluated prior to

assignment to a variable.

Rules for numeric conversion are described on page 115 of the

"Microsoft C Language Reference" manual.

More Information:

The following example demonstrates the overflow condition:

#include <stdio.h>

void main(void);

void main()

{

long l;

l = 70 * 1000; /* First Example */

printf( "l = %ld\n", l ); /* l = 4464 = 70000 % 65536 */

/* Overflow not caught!!! */

l = 70L * 1000; /* Second Example */

printf( "l = %ld\n", l ); /* l = 70000 */

/* arithmetic in long--no */

/* overflow */

}

In the first example, 70 and 1000 are considered as integers. Because

both are integer types, integer math is being performed. Integers can

have at most a value of 32,767. When 70 is multiplied to 1000, the

product exceeds the maximum value that an integer can hold. Overflow

is not checked and information is lost. Thus we get a value of 4464,

which is 70,000 mod 65,536.

The workaround is the second example. Conversions occur if the types

of the operands are different. Note: 70 is a long integer (32 bits;

without the L, it is considered a normal integer of 16 bits). Because

a long integer is used, all operands are converted to long and the

math is done using 32-bit arithmetic. The product is large enough to

handle the multiplication, so the correct result of 70,000 is

generated.

Additional reference words: 5.10 6.00 6.00a 6.00ax 7.00