Versions Compared

Key

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

...

In modwrap semantics (also called modulo arithmetic), integer values "wrap round." That is, adding one 1 to MAX produces MIN. This is the defined behavior for unsigned integers in the C standard Standard [ISO/IEC 9899:2011], Section 6.2.5, paragraph 9. This It is frequently the behavior of signed integers, as well. However, it is more sensible in many applications to use saturation semantics instead of modwrap semantics. For example, in the computation of a size (using unsigned integers), it is often better for the size to stay at the maximum value in the event of overflow rather than suddenly becoming a very small value.

...

Another technique for avoiding integer overflow is to use only half the range of signed integers. For example, when using an int, use only the range [INT_MIN/2, INT_MAX/2]. This has been a trick of the trade in Fortran for some time, and now that optimizing C compilers are more sophisticated, it can be valuable in C.

...

Partially implemented.

Tool

Version

Checker

Description

LDRA tool suite

Include Page
LDRA_VLDRA_V

488 S

Compass/ROSE

 

 

Could detect violations of this recommendation by flagging any comparison expression involving addition that could potentially overflow. For example, instead of comparing a + b < c (where b and c are compile-time constants) and b > c, the code should compare a < c - b. (This assumes a, b, c are unsigned ints. Usually b is small and c is an upper bound such as INT_MAX.)

LDRA tool suite

Include Page
LDRA_V
LDRA_V

488 S

Partially implemented.

PRQA QA-C
Include Page
PRQA_V
PRQA_V

0272 (I)
0273 (I)

Partially implemented.

Related Vulnerabilities

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

Related Guidelines

...

...

Numeric conversion errors

...

[FLC]

Bibliography

...

[

...