CERT-INT30-C_b
In this section:
Synopsis
Ensure that unsigned integer operations do not wrap.
Enabled by default
No
Severity/Certainty
High/High

Full description
Unsigned integer operations can wrap if the resulting value cannot be represented by the underlying representation of the integer. Integer values must not be allowed to wrap. This check warns on other wrapping cases except the ones already covered by CERT-INT30-C_a.
Coding standards
- CERT INT30-C
Ensure that unsigned integer operations do not wrap
Code examples
The following code example fails the check and will give a warning:
void example(unsigned int a, unsigned int b) {
unsigned int usum = a + b;
}
The following code example passes the check and will not give a warning about this issue:
#include <limits.h>
void example(unsigned int a, unsigned int b) {
unsigned int usum;
if (UINT_MAX - a < b) {
/* Handle error */
} else {
usum = a + b;
}
}