...
Attempting to access an object outside of its lifetime could result in an exploitable vulnerability.
Non-Compliant Code Example (
...
Static Variables)
This non-compliant code example declares the variable p
as a pointer to a constant char
with file scope. The value of str
is assigned to p
within the dont_do_this()
function. However, str
has automatic storage duration, so the lifetime of str
ends when the dont_do_this()
function exits.
...
Some compilers generate a warning when a local stack pointer to an automatic variable is returned from a function, as in this example. Compile your code at high warning levels and resolve any warnings (see MSC00-A. Compile cleanly at high warning levels).
...
Correcting this example depends on the intent of the programmer. If the intent is to modify the value of array
and have that modification persist outside of the scope of init_array()
, the desired behavior can be achieved by declaring array
elsewhere and passing it as an argument to init_array()
.
Code Block | ||
---|---|---|
| ||
void init_array(char array[]) { /* Initialize array */ return; } int main(int argc, char *argv[]) { char array[10]; init_array(array); /* ... */ return 0; } void init_array(char array[]) { /* Initialize array */ return; } |
Risk Assessment
Referencing an object outside of its lifetime could result in an attacker being able to run arbitrary code.
...