Versions Compared

Key

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

...

Operator

Wrap

 

Operator

Wrap

 

Operator

Wrap

 

Operator

Wrap

+

yesYes

 

-=yes

Yes

 

<<

yesYes

 

<

noNo

-

yesYes

 

*=

yesYes

 

>>

noNo

 

>

noNo

*

yesYes

 

/=no

No

 

&

noNo

 

>=

noNo

/

noNo

 

%=

noNo

 

|

noNo

 

<=

noNo

%

noNo

 

<<=

yesYes

 

^

noNo

 

==

noNo

++

yesYes

 

>>=

noNo

 

~

noNo

 

!=

noNo

--

yesYes

 

&=

noNo

 

!

noNo

 

&&

noNo

=

noNo

 

|=

noNo

 

un +

noNo

 

||

noNo

+=

yesYes

 

^=

noNo

 

un -

yesYes

 

?:

noNo

Although unsigned left shift << can result in wrapping, modulo behavior is permitted by this standard because of common usage, because this behavior is usually expected by the programmer, and because the behavior is well-defined.

...

This compliant solution performs a precondition test of the operands of the addition to guarantee there is no possibility of unsigned wrap.:

Code Block
bgColor#ccccff
langc
unsigned int ui1, ui2, usum;

/* Initialize ui1 and ui2 */

if (UINT_MAX - ui1 < ui2) {
  /* handle error condition */
}
else {
  usum = ui1 + ui2;
}

...

This compliant solution performs a postcondition test to ensure that the result of the unsigned addition operation usum is not less than the first operand.:

Code Block
bgColor#ccccff
langc
unsigned int ui1, ui2, usum;

/* Initialize ui1 and ui2 */

usum = ui1 + ui2;
if (usum < ui1) {
  /* handle error condition */
}

...

This compliant solution performs a precondition test of the unsigned operands of the subtraction operation to guarantee there is no possibility of unsigned wrap.:

Code Block
bgColor#ccccff
langc
unsigned int ui1, ui2, udiff;

/* Initialize ui1 and ui2 */

if (ui1 < ui2){
   /* handle error condition */
}
else {
  udiff = ui1 - ui2;
}

...

This compliant solution performs a postcondition test that the result of the unsigned subtraction operation udiff is not greater than the minuend.:

Code Block
bgColor#ccccff
langc
unsigned int ui1, ui2, udiff ;

/* Initialize ui1 and ui2 */

udiff = ui1 - ui2;
if (udiff > ui1) {
  /* handle error condition */
}

...

This compliant solution tests the operands of the multiplication to guarantee that there is no unsigned integer wrap.:

Code Block
bgColor#ccccff
langc
pen->num_vertices = _cairo_pen_vertices_needed(
  gstate->tolerance, radius, &gstate->ctm
);

if (pen->num_vertices > SIZE_MAX/sizeof(cairo_pen_vertex_t)) {
  /* handle error condition */
}
pen->vertices = malloc(
  pen->num_vertices * sizeof(cairo_pen_vertex_t)
);

...

This noncompliant code example using atomic integers can result in unsigned integer overflow wrapping.:

 

Code Block
atomic_int i;
int ui1;

/* Initialize i, ui1 */

atomic_fetch_add(&i, ui1);

Compliant Solution

This compliant solution performs a postcondition test to ensure that the result of the unsigned addition operation to i is not less than the operand ui1.:

 

Code Block
atomic_int i;
int ui1;
 
/* Initialize ui1, i */
 
atomic_fetch_add(&i, ui1);
if (atomic_load(&i) < ui1) {
  /* handle error condition */
}

Exceptions

...

Tool

Version

Checker

Description

Compass/ROSE

 

 

Can detect violations of this rule by ensuring that operations are checked for overflow before being performed. Be mindful of exception INT30-EX2 because it excuses many operations from requiring validation, including all the operations that would validate a potentially dangerous operation. For instance, adding two unsigned ints together requires validation involving subtracting one of the numbers from UINT_MAX, which itself requires no validation because it cannot wrap.

Coverity6.5INTEGER_OVERFLOWImplemented

Fortify SCA

5.0

 

Can detect violations of this rule with the CERT C Rule Pack.

PRQA QA-C
Include Page
PRQA_V
PRQA_V

2910 (C)
2911 (D)
2912 (A)
2913 (S)
3302
3303
3304

Partially implemented.

Related Vulnerabilities

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 many bytes of data to another buffer [xorl 2009]. This can cause a buffer overflow, which allows an attacker to execute arbitrary code.

...