...
Code Block |
---|
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.
Code Block |
---|
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.
Code Block |
---|
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.
Code Block |
---|
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.
Code Block |
---|
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.
Code Block |
---|
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.
Code Block |
---|
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.
Code Block |
---|
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.
Code Block |
---|
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;
|