Versions Compared

Key

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

...

Non-Compliant Code Example

This code can result mayresult 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;

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

Non-Compliant Code Example

This code may result in a signed integer oveflow during the addition of the signed operands si1 and si2. If this behavior is unanticipated, it could lead to an exploit vulnerability.

Code Block

int si1, si2, sum;

sum = si1 + si2;

Compliant Solution

This compliant solution tests the suspect addition operation to ensure no overflow occurs.

Code Block

int si1, si2, sum;

if( (((lhs+rhs)^lhs)&((lhs+rhs)^rhs)) >> (sizeof(int)*CHAR_BIT-1) ){
   /* handle error condition */
}

sum = si1 + si2;

Subtraction

Subtraction is between two operands of arithmetic type, two pointers to qualified or unqualified versions of compatible object types, or between a pointer to an object type and an integer type. (Decrementing is equivalent to subtracting one.)

Non-Compliant Code Example

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

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

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

This code may result in an unsigned integer overflow during the subtraction of the unsigned operands ui1 and ui2. If this behavior is unanticipated it may lead to an exploit vulnerability.

Code Block

unsigned int ui1, ui2, result;

result = ui1 - ui2;

Compliant Solution

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

Code Block

unsigned int ui1, ui2, result;

if(ui1 < ui2){
   /* handle error condition */
}

result = ui1 - ui2;

Multiplication

Multiplication is between two operands of arithmetic type.

Non-Compliant Code Example

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

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

...

Code Block
signed int si1, si2, result;

if (si1 > 0){  /* si1 is positive */
  if (si2 > 0) {  /* si1 and si2 are positive */
    if (si1 > (INT_MAX / si2)) {
      /* handle error condition */
    }
  } /* end if si1 and si2 are positive */
  else { /* si1 positive, si2 non-positive */
    if (si2 < (INT_MIN / si1)) {
        /* handle error condition */
    }
  } /* si1 positive, si2 non-positive */
} /* end if si1 is positive */
else { /* si1 is non-positive */
  if (si2 > 0) { /* si1 is non-positive, si2 is positive */
    if (si1 < (INT_MIN / si2)) {
      /* handle error condition */
    }
  } /* end if si1 is non-positive, si2 is positive */
  else { /* si1 and si2 are non-positive */
    if( (si1 != 0) && (si2 < (INT_MAX / si1))) {
      /* handle error condition */
    }
  } /* end if si1 and si2 are non-positive */
} /* end if si1 is non-positive */

result = si1 * si2;

Non-Compliant Code Example

This code may result in an unsigned integer overflow during the multiplication of the operands ui1 and ui2. If unanticipated this overflow may lead to and exploit vulnerability.

Code Block

unsigned int ui1, ui2, result;

result = ui1 * ui2;

Compliant Solution

This compliant solution tests the suspect multiplication operation to guarantee that there is no unsigned integer overflow.

Code Block

unsigned int ui1, ui2, result;

if( ui1 > UMAX_INT/ui2){
   /* handle error condition */
}

result = ui1 * ui2;

Division

Division is between two operands of arithmetic type. Overflow can occur during twos-complement signed integer division when the dividend is equal to the minimum (negative) value for the signed integer type and the divisor is equal to -1. Both signed and unsigned division operations are also susceptible to divide-by-zero errors.

Non-Compliant Code Example

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

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

...

The unary negation operator takes an operand of arithmetic type. Overflow can occur during twos-complement unary negation when the operand is equal to the minimum (negative) value for the signed integer type.

Non-Compliant Code Example

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

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

...

The left shift operator is between two operands of integer type.

Non-Compliant Code Example

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

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

...

The shift operator is between two operands of integer type.

Non-Compliant Code Example

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

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

...