Versions Compared

Key

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

The C Standard, 6.2.5, paragraph 9 [ISO/IEC 9899:2011], states:

A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type.

...

Operator

Wrap

Operator

Wrap

Operator

Wrap

Operator

Wrap

+

Yes

-=

Yes

<<

Yes

<

No

-

Yes

*=

Yes

>>

No

>

No

*

Yes

/=

No

&

No

>=

No

/

No

%=

No

|

No

<=

No

%

No

<<=

Yes

^

No

==

No

++

Yes

>>=

No

~

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 unsigned integer wrap. When operating on integer types with less precision than int, integer promotions are applied. The usual arithmetic conversions may also be applied to (implicitly) convert operands to equivalent types before arithmetic operations are performed. Programmers should understand integer conversion rules before trying to implement secure arithmetic operations. (see See INT02-C. Understand integer conversion rules.).

Integer values must not be allowed to wrap, especially if they are used in any of the following ways:

...

Addition is between two operands of arithmetic type or between a pointer to an object type and an integer type. This rule only applies to only to addition between two operands of arithmetic type. (see See ARR37-C. Do not add or subtract an integer to a pointer to a non-array object and ARR30-C. Do not form or use out-of-bounds pointers or array subscripts.).

Incrementing is equivalent to adding 1.

...

Subtraction is between two operands of arithmetic type, two pointers to qualified or unqualified versions of compatible object types, or a pointer to an object type and an integer type. This rule only applies to only to subtraction between two operands of arithmetic type. (see See ARR36-C. Do not subtract or compare two pointers that do not refer to the same array, ARR37-C. Do not add or subtract an integer to a pointer to a non-array object, and ARR30-C. Do not form or use out-of-bounds pointers or array subscripts for information about pointer subtraction.).

Decrementing is equivalent to subtracting 1.

...

INT30-C-EX3. The left-shift operator takes two operands of integer type. Unsigned left shift << can exhibit modulo behavior (wrapping).  This exception is provided because of common usage , because this behavior is usually expected by the programmer , and because the behavior is well defined. For examples of usage of the left-shift operator, see INT34-C. Do not shift an expression by a negative number of bits or by greater than or equal to the number of bits that exist in the operand.

...

CVE-2009-1385 results from a violation of this rule. The value performs an unchecked subtraction on the length of a buffer and then adds that those many bytes of data to another buffer [xorl 2009]. This can cause a buffer overflow, which allows an attacker to execute arbitrary code.

...