MISRA C:2023 Rule 18.6
The address of an object with automatic or thread-local storage shall not be copied to another object that persists after the first object has ceased to exist
Since R2024a
Description
Rule Definition
The address of an object with automatic storage or thread-local shall not be copied to another object that persists after the first object has ceased to exist.
Rationale
The address of an object becomes indeterminate when the lifetime of that object expires. Any use of an indeterminate address results in undefined behavior.
Polyspace Implementation
Polyspace® reports a violation of this rule when a pointer to a local variable or thread local leaves the scope of the variable. For instance:
A function returns a pointer to a local variable.
A function performs the assignment
globPtr = &locVar
.globPtr
is a global pointer variable andlocVar
is a local variable.A function performs the assignment
*paramPtr = &locVar
.paramPtr
is a function parameter that is, for instance, anint**
pointer andlocVar
is a localint
variable.The address of a thread local variable is assigned to a global pointer.
Polyspace applies this rule on memory allocated using the alloca
function and ignores static local variables. Polyspace assumes that the local objects within a function definition are in the same
scope.
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: Pointers and Arrays |
Category: Required |
AGC Category: Required |