Integer values used in the following manner must be guranteed guaranteed correct:
- As an array index
- In any pointer arithmetic
- As a length or size of an object
- As the bound of an array (for example, a loop counter)
...
The following sections examine specific operations that are susceptible to integer overflow. The specific tests that are required to guarantee that the operation does not result in an integer overflow depend on the signedness of the integer types. When operating on small types (smaller than int
integer convertion conversion rules apply. The usual arithmetic convertions conversions may also be applied to (implicitly) convert operands to equivalent types before arithmetic operations are performed. Make sure you understand implicit conversion rules before trying to implement secure arithmetic operations.
...
Code Block |
---|
signed int si1, si2, result; signed long long tmp = (signed long long)si1 * (signed long long)si2; /* * If the product cannot be repesented as a 32-bit integer handle as an error condition */ if ( (tmp > INT_MAX) || (tmp < INT_MIN) ) { /* handle error condition */ } result = (int)tmp; |
The preceeding preceding code is only compliant on systems where long long
is at least twice the size of int
. On systems where this does not hold the following compliant solution may be used to ensure signed overflow does not occur.
...
Integer overflow can lead to buffer overflows and the execution of arbitary arbitrary code by an attacker.
References
...