Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: link operators in table to guidance for those operators

...

Operator

Overflow

 

Operator

Overflow

 

Operator

Overflow

 

Operator

Overflow

+

yes

 

-=

yes

 

<<

yes

 

<

no

-

yes

 

*=

yes

 

>>

yes

 

>

no

*

yes

 

/=

yes

 

&

no

 

>=

no

/

yes

 

%=

no

 

|

no

 

<=

no

%

no

 

<<=

yes

 

^

no

 

==

no

++

yes

 

>>=

yes

 

~

no

 

!=

no

--

yes

 

&=

no

 

!

no

 

&&

no

=

no

 

|=

no

 

un +

no

 

||

no

+=

yes

 

^=

no

 

un -

yes

 

?:

no

The following sections examine specific operations that are susceptible to integer overflow. The specific tests that are required to guarantee that the operation does not result in an integer overflow depend on the signedness of the integer types. When operating on small types (smaller than int), integer conversion rules apply. The usual arithmetic conversions may also be applied to (implicitly) convert operands to equivalent types before arithmetic operations are performed. Make sure you understand implicit conversion rules before trying to implement secure arithmetic operations.

...

Anchor

...

Addition

...

Addition

Right Shift Operator

Include Page
c:INT32-C-a. Ensure that integer addition operations do not result in an overflow
c:INT32-C-a. Ensure that integer addition operations do not result in an overflow

...

Anchor
Subtraction
Subtraction

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

...

Code Block
unsigned int ui1, ui2, result;

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

result = ui1 - ui2;

...

Anchor
Multiplication
Multiplication

Multiplication

Multiplication is between two operands of arithmetic type.

...

Code Block
unsigned int ui1, ui2, result;

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

result = ui1 * ui2;

...

Anchor
Division
Division

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.

...

Code Block
signed long sl1, sl2, result;

if ( (sl2 == 0) || ( (sl1 == LONG_MIN) && (sl2 == -1) ) ) {
  /* handle error condition */
}
result = sl1 / sl2;

...

Anchor
Unary Negation
Unary Negation

Unary Negation

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.

...

Code Block
signed int si1, result;

if (si1 == INT_MIN) {
  /* handle error condition */
}
result = -si1;

----

Anchor
Left Shift Operator
Left Shift Operator

Left Shift Operator

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

...