Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Wiki Markup
Local, automatic variables can assume _unexpected_ values if they are used before they are initialized. The current C specification statesC99 specifies "If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate." \[[ISO/IEC 9899, 6.7.8 Initialization-1999|AA. C References#ISO/IEC 9899-1999]\] In practice, this value defaults to iswhichever whatvalues everare iscurrently onstored thein stack memory.  While Thisunitialized maymemory causeoften variablescontains tozero, takethis onis unintendednot valuesguaranteed. Consequently, thisThis can consequently cause a program to behave in an unpredictable or unplanned manner, and may provide an avenue for attack. Some compilers warn about unitialized variables, but the warning is inconsistent and these can be ignored by the programmer. As a result, it is necessary to guarantee that all local variables are initialized with a default value. The value assigned should be documented as the "_default value"_ for that variable in the comments associated with that variable's declaration.

Non-

...

Compliant Code Example 1

In this example, two functions are called one after anotherconsecutively. The first function, func1(...) is passed an integer entered by a user. That integer is stored in variable: i for the duration of the function. The second function func2() declares a local integer variable: j. j is not initialized before being checked against a constant value, CONDITION_CHECK. Since j was not initializedBecause j is uninitialized, it assumes whatever value is at that location in the stack, in this case the value of i from func1(). Thus As a result, if the user entered 42, the condition statement if (j == CONDITION_CHECK) succeeds.

...