Versions Compared

Key

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

...

To default-initialize an object of type T means:
— if T is a (possibly cv-qualified) class type, the default constructor for T is called (and the initialization is ill-formed if T has no default constructor or overload resolution results in an ambiguity or in a function that is deleted or inaccessible from the context of the initialization);
— if T is an array type, each element is default-initialized;
— otherwise, no initialization is performed.
If a program calls for the default initialization of an object of a const-qualified type T, T shall be a class type with a user-provided default constructor.

As a result, objects of type T with automatic or dynamic storage duration must be explicitly initialized prior to having their value read as part of an expression, except if T is a class type or array thereof, or an unsigned narrow character type. If T is an unsigned narrow character type, it may be used to initialize an object of unsigned narrow character type, which results in both objects having an indeterminate value. This can be used to implement copy operations like std::memcpy() without triggering undefined behavior.

...

Objects of static or thread storage duration are zero-initialized before any other initialization takes place [ISO/IEC 14882-2014], and need not be explicitly initialized prior to having their value read.

Noncompliant Code Example

In this noncompliant code example, an uninitialized local variable is evaluated as part of an expression to print its value, resulting in undefined behavior:

Code Block
bgColor#FFcccc
langcpp
#include <iostream>
 
void f() {
  int i;
  std::cout << i;
}

Compliant Solution

In this compliant solution, the object is initialized prior to printing its value:

Code Block
bgColor#ccccff
langcpp
#include <iostream>
 
void f() {
  int i = 0;
  std::cout << i;
}

Noncompliant Code Example

In this noncompliant code example, an int * object is allocated by a new-expression, but the memory is points to is not initialized. The object's pointer value, and the value it points to are printed to the standard output stream. Printing the pointer value is well-defined, but attempting to print the value pointed to yields an indeterminate value, resulting in undefined behavior.

Code Block
bgColor#FFcccc
langcpp
#include <iostream>
 
void f() {
  int *i = new int;
  std::cout << i << ", " << *i;
}

Compliant Solution

In this compliant solution, the memory is properly initialized prior to printing its value:

Code Block
bgColor#ccccff
langcpp
#include <iostream>
 
void f() {
  int *i = new int;
  *i = 12;
  std::cout << i << ", " << *i;
}

Noncompliant Code Example

In this noncompliant code example, the class member variable C is not initialized by the default constructor. Despite the local variable o being default-initialized, the use of C within the call to S::f() results in the use of an indeterminate value.

Code Block
bgColor#FFcccc
langcpp
class S {
  int C;
 
public:
  int f(int I) const { return I + C; }
};
 
void f() {
  S o;
  int i = o.f(10);
}

Compliant Solution

In this compliant solution, S is given a default constructor that initializes the class member variable C:

Code Block
bgColor#ccccff
langcpp
class S {
  int C;
 
public:
  S() : C(0) {}
  int f(int I) const { return I + C; }
};
 
void f() {
  S o;
  int i = o.f(10);
}

Risk Assessment

Reading uninitialized variables is undefined behavior and can result in unexpected program behavior. In some cases, these security flaws may allow the execution of arbitrary code.

...

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

EXP33-CPP

High

Probable

Medium

P12

L1

Automated Detection

Tool

Version

Checker

Description

    

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

Related Guidelines

Bibliography

[ISO/IEC 14882-2014]8.5, "Initializers"
Clause 5, "Expressions"
5.3.4, "New"
12.6.2, "Initializing Bases and Members" 
[Lockheed Martin 05]Rule 142, "All variables shall be initialized before use"

...