Unsigned integer constant overflow
Constant value falls outside range of unsigned integer data type
Description
This defect occurs in the following cases:
You assign a compile-time constant to an unsigned integer variable whose data type cannot accommodate the value.
You use an
enum
value that cannot be accommodated by the underlying type of theenum
(and the underlying type is unsigned).
An n
-bit unsigned integer holds values in the range [0,
2n
-1]. For instance,
c
is an 8-bit unsigned char
variable that cannot hold
the value 256.
unsigned char c = 256;
This defect checker depends on the following options:
To determine the sizes of fundamental types, Bug Finder uses your specification for
Target processor type (-target)
.To determine the underlying types of enumerations, Bug Finder uses your specification for
Enum type definition (-enum-type-definition)
.
You do not see the defect in these situations:
Creation of new constants from
const
variables (for specific compilers only).Different compilers might define compile-time constants differently. In the following code,
c+1u
is considered a compile time-constant by GCC compilers, but not by the standard C compiler:Whether you see a violation of this check onconst uint16_t c = 0xffffu; uint16_t y = c + 1u;
y
might depend on your compiler.Bitwise
NOT
operation.Polyspace® does not raise this violation when you perform a bitwise
NOT
operation.
Risk
The C standard states that overflowing unsigned integers must be wrapped around (see, for instance, the C11 standard, section 6.2.5). However, the wrap-around behavior can be unintended and cause unexpected results.
Fix
Check if the constant value is what you intended. If the value is correct, use a wider data type for the variable.
Examples
Result Information
Group: Numerical |
Language: C | C++ |
Default: Off |
Command-Line Syntax:
UINT_CONSTANT_OVFL |
Impact: Low |
Version History
Introduced in R2018b
See Also
Integer overflow
| Integer conversion overflow
| Integer constant
overflow
| Unsigned integer overflow
| Unsigned integer conversion overflow
| Sign change integer conversion overflow
| Find defects (-checkers)
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)