A non-lvalue expression with structure or union type, where the structure or union contains a member with array type (including, recursively, members of all contained structures and unions) refers to an object with automatic storage duration and temporary lifetime. Its lifetime begins when the expression is evaluated and its initial value is the value of the expression. Its lifetime ends when the evaluation of the containing full expression or full declarator ends. Any attempt to modify an object with temporary lifetime results in undefined behavior.
This definition differs from the C99 Standard (which defines modifying the result of a function call or accessing it after the next sequence point as undefined behavior) because a temporary object's lifetime ends when the evaluation containing the full expression or full declarator ends, so the result of a function call can be accessed. This extension to the lifetime of a temporary also removes a quiet change to C90 and improves compatibility with C++.
C functions may not return arrays; however, functions can return a pointer to an array or a
union that contains arrays. Consequently, in any version of C, if a function call returns by value a
union containing an array, do not modify those arrays within the expression containing the function call. In C99 and older, do not access an array returned by a function after the next sequence point or after the evaluation of the containing full expression or full declarator ends.
Noncompliant Code Example
This noncompliant code example conforms to the C11 Standard; however, it fails to conform to C99. If compiled with a C99-conforming implementation, this code has undefined behavior because the sequence point preceding the call to
printf() comes between the call and the access by
printf() of the string in the returned object.
Compliant Solution (C11 and newer)
This compliant solution checks
__STDC_VERSION__ to ensure that a pre-C11 compiler will fail to compile the code, rather than invoking undefined behavior.
This compliant solution stores the structures returned by the call to
addressee() before calling the
printf() function. Consequently, this program conforms to both C99 and C11.
Noncompliant Code Example
This noncompliant code example attempts to retrieve an array and increment the array's first value. The array is part of a
struct that is returned by a function call. Consequently, the array has temporary lifetime, and modifying the array is undefined behavior in both C99 and C11.
This compliant solution stores the structure returned by the call to
my_x before calling the
printf() function. When the array is modified, its lifetime is no longer temporary but matches the lifetime of the block in
Attempting to modify an array or access it after its lifetime expires may result in erroneous program behavior.
|Axivion Bauhaus Suite|
|LDRA tool suite|
|642 S, 42 D, 77 D||Enhanced Enforcement|
|Do not access an array in the result of a function call|
|CERT-C: Rule EXP35-C||Checks for accesses on objects with temporary lifetime (rule fully covered)|
0450 [U], 0455 [U], 0459 [U],
0464 [U], 0465 [U]
Key here (explains table format and definitions)
|ISO/IEC TR 24772:2013||Dangling References to Stack Frames [DCM]||Prior to 2018-01-12: CERT: Unspecified Relationship|
|ISO/IEC TR 24772:2013||Side-effects and Order of Evaluation [SAM]||Prior to 2018-01-12: CERT: Unspecified Relationship|
|[ISO/IEC 9899:2011]||6.2.4, "Storage Durations of Objects"|