MISRA C++:2023 Rule 4.1.3
There shall be no occurrence of undefined or critical unspecified behaviour
Since R2024b
Description
Rule Definition
There shall be no occurrence of undefined or critical unspecified behaviour.
Rationale
C++ code that contains instances of undefined or critical unspecified behavior can produce unexpected or incorrect results. Such code can behave differently in different implementations. Issues caused by undefined behavior in the code can be difficult to analyze because the compiler might optimize the code assuming that undefined behavior does not occur. In addition, these behaviors can be difficult to detect during testing because the code might behave as expected for a given set of test data.
Note
Many MISRA C++:2023 rules address specific undefined or critical unspecified behaviors. This rule applies to any undefined or critical unspecified behavior that is not addressed in any other rule.
Polyspace Implementation
Polyspace® reports violations on these instances of undefined or critical unspecified behavior:
Undefined uses of
offsetof
:Use of
offsetof
on bit fieldsUse of
offsetof
when the second argument is not a structure field of the first argument
Undefined uses of a shift operator:
Use of a negative operand on the right side of a shift operator
Use of a shift operator with a right operand that is at least as large as the bit size of the data type of the left operand
Occurrences of integer constant overflow:
Assigning a compile-time integer constant to a signed integer variable whose data type cannot accommodate the value.
Use of an enumeration value that cannot be accommodated by the underlying type of the enumeration, when the underlying type is signed.
Performing a binary operation involving two integer constants that results in an overflow. That is, the result 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
.
Undefined uses of the
defined
operator:Use of
defined
without an identifierUse of a macro whose expansion contains the
defined
operator
Use of an array of incomplete types
Use of a function-like macro that provides an incorrect number of arguments
Troubleshooting
If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: General principles |
Category: Required |
Version History
Introduced in R2024b