...
This noncompliant code example conforms with the C11 standard; however, it fails to conform with C99. If compiled with a C99-conforming implementation, this code demonstrates 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:
...
This compliant solution stores the structures returned by the call to addressee() before calling the printf() function: . Consequently, this program conforms with both C99 and C11.
...
The following 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 results in undefined behavior.
| Code Block | ||||
|---|---|---|---|---|
| ||||
#include <stdio.h>
struct X { int a[6]; };
struct X addressee(void) {
struct X result = { { 1, 2, 3, 4, 5, 6 } };
return result;
}
int main(void) {
printf("%x", ++(addressee().a[0]));
return 0;
}
|
...
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
...