You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 15 Next »

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:

Operator

Overflow

 

Operator

Overflow

 

Operator

Overflow

 

Operator

Overflow

+

yes

 

-=

yes

 

<<

yes

 

<

no

-

yes

 

*=

yes

 

>>

yes

 

>

no

*

yes

 

/=

yes

 

&

no

 

>=

no

/

yes

 

%=

yes

 

|

no

 

<=

no

%

yes

 

<<

yes

 

^

no

 

==

no

++

yes

 

>>=

yes

 

~

no

 

!=

no

-

yes

 

&=

no

 

!

no

 

&&

no

=

no

 

|=

no

 

un +

no

 

||

no

+=

yes

 

^=

no

 

un -

yes

 

?:

no

Addition

Addition in C is between two operands of arithmetic type, or between a pointer to an object type and an integer type. (Incrementing is equivalent to adding one.)

Non-compliant Code Example

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

unsigned int sum;
unsigned int ui1 = UINT_MAX;
unsigned int ui2 = 1;

sum = ui1 + ui2;

Compliant Solution

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.

unsigned int sum;
unsigned int ui1 = UINT_MAX;
unsigned int ui2 = 1;

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

Subtraction

Subtraction in C 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

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.

signed int result;
signed int si1 = INT_MAX;
signed int si2 = -1;

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.

signed int result;
signed int si1 = INT_MAX;
signed int si2 = -1;

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

Multiplication

Multiplication in C is between two operands of arithmetic type.

Non-compliant Code Example

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.

signed int result;
signed int si1 = INT_MIN;
signed int si2 = 2;

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.

signed int result;
signed int si1 = INT_MIN;
signed int si2 = 2;
signed long long tmp = (signed long long)lhs * (signed long long)rhs;

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 atleast 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.

signed int result;
signed int si1 = INT_MIN;
signed int si2 = 2;

if( si1> 0){
	if( si2> 0){
		if(si1> (INT_MAX / si2))
		        error_handler("OVERFLOW ERROR", NULL, EOVERFLOW);
	} else {
		if ( si2< (INT_MIN / si1))
			error_handler("OVERFLOW ERROR", NULL, EOVERFLOW);
	}
} else {
	if(si2 > 0){
		if( si1 < (INT_MIN / si2))
			error_handler("OVERFLOW ERROR", NULL, EOVERFLOW);
	} else {
		if( (si1 != 0) && (si2 < (INT_MAX / si1)))
			error_handler("OVERFLOW ERROR", NULL, EOVERFLOW);
	}
}

result = si1 * si2;

Division

Division in C is between two operands of arithmetic type.

Non-compliant Code Example

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

signed long sl1 = LONG_MIN;
signed long sl2 = -1;
signed long result;

result = sl1 / sl2;

Compliant Solution

The following compliant solution tests the suspect division 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.

signed long sl1 = LONG_MIN;
signed long sl2 = -1;
signed long result;

if( (sl2 == 0) || ( (sl1 == LONG_MIN) && (sl2 == -1) ) ){
	error_handler("ERROR OVERFLOW", NULL, EOVERFLOW);
	return 0;
}
result = sl1 / sl2;

Modulo

Modulo in C is between two operands of integer type.

Non-compliant Code Example

The following code will result in a signed integer overflow during the 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.

signed long sl1 = LONG_MIN;
signed long sl2 = 0;
signed long result;

result = sl1 % sl2;

Compliant Solution

The following compliant solution tests the suspect Modulo 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.

signed long sl1 = LONG_MIN;
signed long sl2 = 0;
signed long result;

if( sl2 == 0){
	error_handler("ERROR OVERFLOW", NULL, EOVERFLOW);
	return 0;
}
result = sl1 % sl2;

Unary Negation

The unary negation operator in C takes an operand of arithmetic type.

Non-compliant Code Example

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

signed int result;
signed int si1 = INT_MIN;

result = -si1;

Compliant Solution

The following compliant solution tests the suspect negation 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.

signed int result;
signed int si1 = INT_MIN;

if (si1== INT_MIN) error_handler("OVERFLOW ERROR", NULL, EOVERFLOW);
return -si1;

Left Shift Operator

The shift operator in C is between two operands of integer type.

Non-compliant Code Example

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

unsigned int result;
unsigned int ui1 = UINT_MAX;
unsigned int ui2 = sizeof(int)*8;

result = ui1 << ui2;

Compliant Solution

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.

unsigned int result;
unsigned int ui1 = UINT_MAX;
unsigned int ui2 = sizeof(int)*8;

if( (ui2 < 0) || (ui2 >= sizeof(int)*8))
	error_handler("OVERFLOW ERROR", NULL, EINVAL);
return ui1 << ui2;

Right Shift Operator

The shift operator in C is between two operands of integer type.

Non-compliant Code Example

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

unsigned int result;
unsigned int ui1 = UINT_MAX;
unsigned int ui2 = sizeof(int)*8;

result = ui1 >> ui2;

Compliant Solution

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.

unsigned int result;
unsigned int ui1 = UINT_MAX;
unsigned int ui2 = sizeof(int)*8;

if( (ui2 < 0) || (ui2 >= sizeof(int)*8))
	error_handler("OVERFLOW ERROR", NULL, EINVAL);
return ui1 >> ui2;
  • No labels