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. C99 specifies, "If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate" \[[ISO/IEC 9899:199914882-2003|AA. C++ References#ISO/IEC 989914882-19992003]\] Section 8. In5, paragraph 9 says: "... if no initializer is specified for a nonstatic object, the common caseobject and its subobjects, onif architecturesany, thathave makean useindeterminate ofinitial avalue". programIn stackpractice, this value defaults to whichever values are currently stored in stack memory. While uninitialized memory often contains zeroeszero, this is not guaranteed. Consequently, uninitialized memory can cause a program to behave in an unpredictable or unplanned manner and may provide an avenue for attack.

...

Code Block
bgColor#ccccff
enum {max_buffer = 24};

void report_error(const char *msg) {
  const char *error_log = msg;
  char buffer[max_buffer];

  snprintf(buffer, sizeof( buffer), "Error: %s", error_log);
  printf("%s\n", buffer)cout << buffer << endl;
}

This solution is compliant provided that the null-terminated byte string referenced by msg is 17 bytes or less, including the null terminator.

Compliant Solution

A much simpler, less error prone, and better performing compliant solution is shown below.

Code Block
bgColor#ccccff
void report_error(const char *msg) {
  cout << printf("Error: %s\n", << msg) << endl;
}

Risk Assessment

Accessing uninitialized variables generally leads to unexpected program behavior. In some cases these types of flaws may allow the execution of arbitrary code.

...

Wiki Markup
\[[Flake 06|AA. C++ References#Flake 06]\]
\[[ISO/IEC 9899:199914882-2003|AA. C++ References#ISO/IEC 989914882-19992003]\] Section 8.5 6.7.8, "Initialization"Initializers.
\[[Lockheed Martin 05|AA. C++ References#Lockheed Martin 05]\] AV Rule 142 All variables shall be initialized before use.
\[[ISO/IEC PDTR 24772|AA. C++ References#ISO/IEC PDTR 24772]\] "LAV Initialization of Variables"
\[[mercy 06|AA. C++ References#mercy 06]\]

...