Accessing object with temporary lifetime
Read or write operations on the object are undefined behavior
Description
This defect occurs when you attempt to read from or write to an object with temporary lifetime that is returned by a function call. In a structure or union returned by a function, and containing an array, the array members are temporary objects. The lifetime of temporary objects ends:
When the full expression or full declarator containing the call ends, as defined in the C11 Standard.
After the next sequence point, as defined in the C90 and C99 Standards. A sequence point is a point in the execution of a program where all previous evaluations are complete and no subsequent evaluation has started yet.
For C++ code, Accessing object with temporary lifetime raises a defect only when you write to an object with a temporary lifetime.
If the temporary lifetime object is returned by address, no defect is raised.
Risk
Modifying objects with temporary lifetime is undefined behavior and can cause abnormal program termination and portability issues.
Fix
Assign the object returned from the function call to a local variable. The content of the temporary lifetime object is copied to the variable. You can now modify it safely.
Examples
Result Information
Group: Programming |
Language: C | C++ |
Default: On for handwritten code, off for generated code |
Command-Line Syntax:
TEMP_OBJECT_ACCESS |
Impact: Low |
Version History
Introduced in R2018a
See Also
Find
defects (-checkers)
| Misuse of structure with flexible array
member
| Write without a
further read
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)