AUTOSAR C++14 Rule A3-8-1
Description
Rule Definition
An object shall not be accessed outside of its lifetime.
Rationale
The lifetime of an object begins when it is created by its constructor. The lifetime ends when the object is deleted. Accessing a variable before its construction or after its destruction can lead to undefined behavior. Depending on the context, many operations might inadvertently access an object outside its lifetime. Examples of such operations include:
Noninitialized pointer: You might inadvertently access a pointer before assigning an address to it. This operation accesses an object before its lifetime and results in accessing an unpredictable memory location. The best practice is to initiate a pointer by using
nullptr
during its declaration.Noninitialized variable: You might inadvertently read a variable before it is initialized. This operation accesses an object before its lifetime and results in reading a garbage value that is unpredictable and useless. The best practice is to initiate a variable during its declaration.
Use of previously deallocated pointer: You might access the dynamically allocated memory of a pointer after deallocating the memory. Trying to access this block of memory accesses an object after its lifetime and results in unpredictable behavior or even a segmentation fault. To address this issue, set the deallocated pointer to
nullptr
, and then to check if a pointer isnullptr
before accessing it. Alternatively, use astd::unique_ptr
instead of a raw pointer. Because you do not need to deallocate the allocated memory for astd::unique_ptr
explicitly, you can avoid inadvertently accessing the deallocated memory.Pointer or reference to stack variable leaving scope: You might assign a nonlocal pointer to a local object. For instance:
A nonlocal or global pointer is assigned to a variable that is local to a function.
A passed-by-reference function parameter, such as a pointer, is assigned to a variable that is local to a function.
A pointer data member of a class is assigned to a variable that is local to a function.
Once the local variable goes out of scope, their corresponding memory blocks might hold garbage or unpredictable values. Accessing pointers to these memory locations accesses an object after its lifetime and might result in undefined or unpredictable behavior. The best practice is to not assign nonlocal pointers to local objects.
Modifying object with temporary lifetime: You might attempt to modify a temporary object returned by a function call. Modifying temporary objects is an undefined behavior that might lead to abnormal program termination depending on the hardware and software that you use. The best practice is to assign the temporary objects in local variables, and then modifying the local variables.
Avoid operations that might access an object outside of its lifetime.
Polyspace Implementation
Polyspace® checks for these scenarios where an object might be accessed outside of its lifetime:
Noninitialized pointer: Polyspace flags a pointer if it is not assigned an address before it is accessed.
Noninitialized variable: Polyspace flags a variable if it is not initialized before its value is read.
Use of previously deallocated pointer: Polyspace flags an operation where you access a block of memory after deallocating the block, for instance, by using the
free()
function or thedelete
operator.Pointer or reference to stack variable leaving scope: Polyspace flags a local variable when a pointer or reference to it leaves its scope. For example, a local variable is flagged when:
A function returns a pointer to the local variable
A global pointer is pointed to the local variable
A pass-by-reference function parameter, such as a pointer, is pointed to the local variable
A pointer data member of a class is pointed to the local variable
Polyspace assumes that the local objects within a function definition are in the same scope.
Accessing object with temporary lifetime: Polyspace flags an operation where you access a temporary object that is returned by a function call.
Extend Checker
You can extend the checker in the following ways:
Polyspace does not flag passing pointers to noninitialized variables to functions. To detect noninitialized variables that are passed to functions by pointers, extend the checker by using the option
-code-behavior-specification
. See Extend Checkers for Initialization to Check Function Arguments Passed by Pointers.If a variable in your code is non-initialized only for certain system input values, you can see one possible combination of input values causing the defect. See Extend Bug Finder Checkers to Find Defects from Specific System Input Values.
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: Basic concepts |
Category: Required, Non-automated |