Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • 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)  

Most integer operations can result in overflow if the resulting value cannot be represented by the underlying representation of the integer. The following table indicates which operators can result in overflow:

...

Non-compliant Code Example

The following code will can result in an unsigned integer overflow during the addition of the unsigned operands ui1 and ui2. If this behavior is unanticipated, the resulting value may be used to allocate insufficient memory for a subsequent operation or in some other manner which could lead to an exploitable vulnerability.

...

The following compliant solution tests the suspect addition operation to guarantee there is no possibility of unsigned overflow. In this particular case, an overflow condition is present and the error_handler() method is invoked.

Code Block
unsigned int ui1, ui2, sum;

if (~ui1 < ui2) {
  /* handle error condition */
}
sum = ui1 + ui2;

...

Non-compliant Code Example

The following code will can result in a signed integer overflow during the subtraction of the signed operands si1 and si2. If this behavior is unanticipated, the resulting value may be used to allocate insufficient memory for a subsequent operation or in some other manner which could lead to an exploitable vulnerability.

...

The following compliant solution tests the suspect subtraction operation to guarantee there is no possibility of signed overflow. In this particular case, an overflow condition is present and the error_handler() method is invoked.

Code Block
signed int si1, si2, result;

if ( ((si1^si2)&((si1-si2)^si1)) >> (sizeof(int)*CHAR_BIT-1) ) {
  /* handle error condition */
}
result = si1 - si2;

...

Non-compliant Code Example

The following code will can result in a signed integer overflow during the multiplication of the signed operands si1 and si2. If this behavior is unanticipated, the resulting value may be used to allocate insufficient memory for a subsequent operation or in some other manner which could lead to an exploitable vulnerability.

...

The following compliant solution tests the suspect multiplication operation to guarantee there is no possibility of signed overflow. In this particular case, an overflow condition is present and the error_handler() method is invoked.

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
 * then handle as an error condition
 */
if ( (tmp > INT_MAX) || (tmp < INT_MIN) ) {
  /* handle error condition */
}
result = (int)tmp;

...

Non-compliant Code Example

The following code will can result in a signed integer overflow during the division of the signed operands sl1 and sl2. If this behavior is unanticipated, the resulting value may be used to allocate insufficient memory for a subsequent operation or in some other manner which could lead to an exploitable vulnerability.

...

Non-compliant Code Example

The following code will can result in a signed integer overflow during the Modulo modulo operation on the signed operands sl1 and sl2. If this behavior is unanticipated, the resulting value may be used to allocate insufficient memory for a subsequent operation or in some other manner which could lead to an exploitable vulnerability.

...

The following compliant solution tests the suspect Modulo modulo operation to guarantee there is no possibility of signed overflow.

...

Non-compliant Code Example

The following code will can result in a signed integer overflow during the unary negation of the signed operand si1. If this behavior is unanticipated, the resulting value may be used to allocate insufficient memory for a subsequent operation or in some other manner which could lead to an exploitable vulnerability.

...

Non-compliant Code Example

The following code will can result in an unsigned overflow during the shift operation of the unsigned operands ui1 and ui2. If this behavior is unanticipated, the resulting value may be used to allocate insufficient memory for a subsequent operation or in some other manner which could lead to an exploitable vulnerability.

...

The following compliant solution tests the suspect shift operation to guarantee there is no possibility of unsigned overflow. In this particular case, an overflow condition is present and the error_handler() method is invoked.

Code Block
unsigned int ui1, ui2, result;

if ( (ui2 < 0) || (ui2 >= sizeof(int)*8) ) {
  /* handle error condition */
}
result = ui1 << ui2;

...

Non-compliant Code Example

The following code will can result in an unsigned overflow during the shift operation of the unsigned operands ui1 and ui2. If this behavior is unanticipated, the resulting value may be used to allocate insufficient memory for a subsequent operation or in some other manner which could lead to an exploitable vulnerability.

...