Versions Compared

Key

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

Section 6.2.5, para. paragraph 9, and of the C standard Standard [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.

...

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

Atomic Integers

The C standard [ISO/IEC 9899:2011] Standard defines arithmetic on atomic integer types as read-modify-write operations with the same representation as nonatomic integer types. As a result, wrapping of atomic unsigned integers is identical to nonatomic unsigned integers and should also be prevented or detected.

...

Tool

Version

Checker

Description

Fortify SCA

V. 5.0

 

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

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.

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.

...

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

Related Guidelines

...

...

Wrap-around error

...

...

Integer overflow (wrap or wraparound)

...

Bibliography

[Dowd 2006]Chapter 6, "C Language Issues" ("Arithmetic

...

Boundary Conditions," pp. 211–223)
[ISO/IEC 9899:2011]Section 6.2.5, "Types"
[Seacord 2005]Chapter 5, "Integers"
[Viega 2005]Section 5.2.7, "Integer

...

...

...