Versions Compared

Key

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

...

Non-Compliant Code Example

The following This code 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 that could lead to an exploitable vulnerability.

Code Block
unsigned int ui1, ui2, sum;

sum = ui1 + ui2;

Compliant Solution

The following This compliant solution tests the suspect addition operation to guarantee there is no possibility of unsigned overflow.

...

Non-Compliant Code Example

The following This code 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 that could lead to an exploitable vulnerability.

Code Block
signed int si1, si2, result;

result = si1 - si2;

Compliant Solution

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

...

Non-Compliant Code Example

The following This code 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 that could lead to an exploitable vulnerability.

Code Block
signed int si1, si2, result;

result = si1 * si2;

Compliant Solution

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

...

Non-Compliant Code Example

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

Code Block
signed long sl1, sl2, result;

result = sl1 / sl2;

Compliant Solution

The following This compliant solution tests the suspect division operation to guarantee there is no possibility of signed overflow or divide-by-zero errors.

...

Non-Compliant Code Example

The following This code 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 that could lead to an exploitable vulnerability.

Code Block
signed int si1, result;

result = -si1;

Compliant Solution

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

...

Non-Compliant Code Example

The following This code 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 that could lead to an exploitable vulnerability.

Code Block
unsigned int ui1, ui2, result;

result = ui1 << ui2;

Compliant Solution

The following This compliant solution tests the suspect shift operation to guarantee there is no possibility of unsigned overflow.

...

Non-Compliant Code Example

The following This code 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 that could lead to an exploitable vulnerability.

Code Block
unsigned int ui1, ui2, result;

result = ui1 >> ui2;

Compliant Solution

The following This compliant solution tests the suspect shift operation to guarantee there is no possibility of unsigned overflow.

...