Integer precision exceeded
Operation using integer size instead of precision can cause undefined behavior
Description
This defect occurs when an integer expression uses the integer size in an operation that exceeds the integer precision. On some architectures, the size of an integer in memory can include sign and padding bits. On these architectures, the integer size is larger than the precision which is just the number of bits that represent the value of the integer.
Risk
Using the size of an integer in an operation on the integer precision can result in integer overflow, wrap around, or unexpected results. For instance, an unsigned integer can be stored in memory in 64 bits, but uses only 48 bits to represent its value. A 56 bits left-shift operation on this integer is undefined behavior.
Assuming that the size of an integer is equal to its precision can also result in program portability issues between different architectures.
Fix
Do not use the size of an integer instead of its precision. To determine the integer
precision, implement a precision computation routine or use a builtin function such as
__builtin_popcount()
.
Examples
Result Information
Group: Numerical |
Language: C | C++ |
Default: Off |
Command-Line Syntax:
INT_PRECISION_EXCEEDED |
Impact: Low |
Version History
Introduced in R2018b
See Also
Bitwise operation on negative
value
| Possible invalid operation on boolean operand
| Integer conversion
overflow
| Integer
overflow
| Shift of a negative
value
| Right operand of shift operation outside allowed
bounds
| Unsigned integer conversion
overflow
| Unsigned integer
overflow
| MISRA C:2012 Rule
10.1
| MISRA C:2012 Rule
10.2
| 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)