Use of Uninitialized Variable

The code uses a variable that has not been initialized, leading to unpredictable or unintended results.


In some languages such as C and C++, stack variables are not initialized by default. They generally contain junk data with the contents of stack memory before the function was invoked. An attacker can sometimes control or read these contents. In other languages or conditions, a variable that is not explicitly initialized can be given a default value that has security implications, depending on the logic of the program. The presence of an uninitialized variable can sometimes indicate a typographic error in the code.


The following examples help to illustrate the nature of this weakness and describe methods or techniques which can be used to mitigate the risk.

Note that the examples here are by no means exhaustive and any given weakness may have many subtle varieties, each of which may require different detection methods or runtime controls.

Example One

This code prints a greeting using information stored in a POST request:

if (isset($_POST['names'])) {
  $nameArray = $_POST['names'];
echo "Hello " . $nameArray['first'];

This code checks if the POST array 'names' is set before assigning it to the $nameArray variable. However, if the array is not in the POST request, $nameArray will remain uninitialized. This will cause an error when the array is accessed to print the greeting message, which could lead to further exploit.

Example Two

The following switch statement is intended to set the values of the variables aN and bN before they are used:

int aN, Bn;
switch (ctl) {

  case -1:
    aN = 0;
    bN = 0;

  case 0:
    aN = i;
    bN = -i;

  case 1:
    aN = i + NEXT_SZ;
    bN = i - NEXT_SZ;

    aN = -1;
    aN = -1;

repaint(aN, bN);

In the default case of the switch statement, the programmer has accidentally set the value of aN twice. As a result, bN will have an undefined value. Most uninitialized variable issues result in general software reliability problems, but if attackers can intentionally trigger the use of an uninitialized variable, they might be able to launch a denial of service attack by crashing the program. Under the right circumstances, an attacker may be able to control the value of an uninitialized variable by affecting the values on the stack prior to the invocation of the function.

See Also

SFP Secondary Cluster: Glitch in Computation

This category identifies Software Fault Patterns (SFPs) within the Glitch in Computation cluster (SFP1).

7PK - Code Quality

This category represents one of the phyla in the Seven Pernicious Kingdoms vulnerability classification. It includes weaknesses that do not directly introduce a weakne...

Comprehensive CWE Dictionary

This view (slice) covers all the elements in CWE.

Weaknesses Introduced During Implementation

This view (slice) lists weaknesses that can be introduced during implementation.

Weaknesses in Software Written in PHP

This view (slice) covers issues that are found in PHP programs that are not common to all languages.

Common Weakness Enumeration content on this website is copyright of The MITRE Corporation unless otherwise specified. Use of the Common Weakness Enumeration and the associated references on this website are subject to the Terms of Use as specified by The MITRE Corporation.