MISRA C:2023 Rule 12.4
Evaluation of constant expressions should not lead to unsigned integer wrap-around
Since R2024a
Description
Rule Definition
Evaluation of constant expressions should not lead to unsigned integer wrap-around1 .
Rationale
Unsigned integer expressions do not strictly overflow, but instead wraparound. Although there may be good reasons to use modulo arithmetic at run time, intentional use at compile time is less likely and might imply logic errors.
Polyspace Implementation
Polyspace® flags the constant expressions that might wraparound.
Different compilers might define compile-time constants differently. In the
                following code, c+1u is considered a constant expression by GCC
                compilers, but not by the standard C
                compiler.
const uint16_t c = 0xffffu; uint16_t y = c + 1u;
Polyspace does not report a violation when a bitwise shift of 1U
        could result in a wraparound.
Troubleshooting
If you expect a rule violation but do not see it, refer to Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
| Group: Expressions | 
| Category: Advisory | 
| AGC Category: Advisory | 
Version History
Introduced in R2024a
See Also
1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.
The MISRA coding standards referenced in the Polyspace Bug Finder™ documentation are from the following MISRA standards:
- MISRA C:2004 
- MISRA C:2012 
- MISRA C:2023 
- MISRA C++:2008 
- MISRA C++:2023 
MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.