Integer constant overflow
Constant value falls outside range of integer data type
Description
This defect occurs in the following cases:
You assign a compile-time integer constant to a signed 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 signed). For most C compilers, the default underlying type issigned int
(based on the C standard).You perform a binary operation involving two integer constants that results in an overflow, that is, a value outside the range allowed by the data type that the operation uses. A binary operation with integer constants uses the
signed int
data type (unless you use modifiers such asu
orL
).
An n
-bit signed integer holds values in the range
[-2n
-1,
2n
-1-1]. For instance,
c
is an 8-bit signed char
variable that cannot hold
the value 255.
signed char c = 255;
This defect checker depends on the following options:
Target processor type (-target)
: Determines the sizes of fundamental types.Enum type definition (-enum-type-definition)
: Determines the underlying types of enumerations.Compiler (-compiler)
: Impacts the interpretation of code.
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+1
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 int16_t c = 32767; int16_t y = c + 1;
y
might depend on your compiler.Bitwise
NOT
operation.Polyspace® does not raise this violation when you perform a bitwise
NOT
operation.
Risk
The default behavior for constant overflows can vary between compilers and platforms. Retaining constant overflows can reduce the portability of your code.
Even if your compilers wraps around overflowing constants with a warning, 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 different, possibly wider, data type for the variable.
Examples
Result Information
Group: Numerical |
Language: C | C++ |
Default: Off |
Command-Line Syntax:
INT_CONSTANT_OVFL |
Impact: Medium |
Version History
Introduced in R2018b
See Also
Integer overflow
| Integer conversion overflow
| Unsigned integer overflow
| Unsigned integer conversion overflow
| Unsigned integer constant
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)