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 exist1 .
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 or thread local variable escapes 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 |
Version History
Introduced in R2024a1 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.