01.07.2016 Views

SEI CERT C Coding Standard

tqcylJ

tqcylJ

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Declarations and Initialization (DCL) - DCL30-C. Declare objects with appropriate storage durations<br />

3 Declarations and Initialization (DCL)<br />

3.1 DCL30-C. Declare objects with appropriate storage durations<br />

Every object has a storage duration that determines its lifetime: static, thread, automatic, or allocated.<br />

According to the C <strong>Standard</strong>, 6.2.4, paragraph 2 [ISO/IEC 9899:2011],<br />

The lifetime of an object is the portion of program execution during which storage is<br />

guaranteed to be reserved for it. An object exists, has a constant address, and retains its<br />

last-stored value throughout its lifetime. If an object is referred to outside of its lifetime,<br />

the behavior is undefined. The value of a pointer becomes indeterminate when the object<br />

it points to reaches the end of its lifetime.<br />

Do not attempt to access an object outside of its lifetime. Attempting to do so is undefined behavior<br />

and can lead to an exploitable vulnerability. (See also undefined behavior 9 in the C <strong>Standard</strong>,<br />

Annex J.)<br />

3.1.1 Noncompliant Code Example (Differing Storage Durations)<br />

In this noncompliant code example, the address of the variable c_str with automatic storage duration<br />

is assigned to the variable p, which has static storage duration. The assignment itself is<br />

valid, but it is invalid for c_str to go out of scope while p holds its address, as happens at the<br />

end of dont_do_this().<br />

#include <br />

const char *p;<br />

void dont_do_this(void) {<br />

const char c_str[] = "This will change";<br />

p = c_str; /* Dangerous */<br />

}<br />

void innocuous(void) {<br />

printf("%s\n", p);<br />

}<br />

int main(void) {<br />

dont_do_this();<br />

innocuous();<br />

return 0;<br />

}<br />

<strong>SEI</strong> <strong>CERT</strong> C <strong>Coding</strong> <strong>Standard</strong>: Rules for Developing Safe, Reliable, and Secure Systems 32<br />

Software Engineering Institute | Carnegie Mellon University<br />

[DISTRIBUTION STATEMENT A] Approved for public release and unlimited distribution.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!