Versions Compared

Key

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

...

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 sum;
unsigned int ui1 = UINT_MAX;
unsigned intui1, ui2, = 1sum;

if (~ui1 < ui2){
  error_handler("Overflow Error", NULL, EOVERFLOW);
}
sum = ui1 + ui2;

...

The following code will 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.

Code Block
signed int result;
signed int si1 = INT_MAX;
signed int si2 = -1si1, si2, result;

result = si1 - si2;

Compliant Solution

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 result;
signed int si1 = INT_MAX;
signed int si2 = -1, si2, result;

if ( ((si1^si2)&((si1-si2)^si1)) >> (sizeof(int)*CHAR_BIT-1) ) {
	  error_handler("OVERFLOW ERROR", NULL, EOVERFLOW);
}
result = si1 - si2;

...

The following code will 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.

Code Block
signed int result;
signed int si1 = INT_MIN;
signed intsi1, si2, = 2result;

result = si1 * si2;

Compliant Solution

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 result;
signed int si1, = INT_MIN;
signed int si2 = 2;si2, result;

signed long long tmp = (signed long long)lhssi1 * (signed long long)rhssi2;

if ( (tmp > INT_MAX) || (tmp < INT_MIN) ) {
  /* The product cannot fit in a 32-bit int */
  error_handler("OVERFLOW ERROR", NULL, EOVERFLOW);
}
result = (int)tmp;

It is important to note that the above 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.

Code Block
signed int result;
signed int si1 = INT_MIN;
signed int si2 = 2, si2, result;

if (si1 > 0){  /* si1 is positive */
  if (si2 > 0) {  /* si1 and si2 are positive */
    if (si1 > (INT_MAX / si2)) { 
      error_handler("OVERFLOW ERROR", NULL, EOVERFLOW);
    }
  } /* end if si1 and si2 are positive */
  else { /* si1 positive, si2 non-positive */
    if (si2 < (INT_MIN / si1)) {
        error_handler("OVERFLOW ERROR", NULL, EOVERFLOW);
    }
  } /* 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)) {
      error_handler("OVERFLOW ERROR", NULL, EOVERFLOW);
    }
  } /* end if si1 is non-positive, si2 is positive */
  else { /* si1 and si2 are non-positive */
    if( (si1 != 0) && (si2 < (INT_MAX / si1))) {
      error_handler("OVERFLOW ERROR", NULL, EOVERFLOW);
    }
  } /* end if si1 and si2 are non-positive */
} /* end if si1 is non-positive */

result = si1 * si2;

Division

...