CWE-457: Use of Uninitialized Variable

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

Extended Description:
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.

Affected languages: C, C++, Perl, PHP, Not Language-Specific
Parent weaknesses: CWE-908, CWE-665, CWE-665

Consequences:
  - Availability: Other
  - Authorization: Other

Detection methods:
  - Fuzzing: Fuzz testing (fuzzing) is a powerful technique for generating large numbers of diverse inputs - either randomly or algorithmically - and dynamically invoking the code with those inputs. Even with rand
  - Automated Static Analysis: Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without havin

Mitigations:
  - [Implementation] Ensure that critical variables are initialized before first use [REF-1485].
  - [Build and Compilation] Most compilers will complain about the use of uninitialized variables if warnings are turned on.
  - [Implementation] When using a language that does not require explicit declaration of variables, run or compile the software in a mode that reports undeclared or unknown variables. This may indicate the presence of a typographic error in the variable's name.
  - [Requirements] Choose a language that is not susceptible to these issues.

Demonstrative examples:
This code prints a greeting using information stored in a POST request:
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.
if (isset($_POST['names'])) { $nameArray = $_POST['names']; } echo "Hello " . $nameArray['first'];

The following switch statement is intended to set the values of the variables aN and bN before they are used:
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.
int aN, Bn; switch (ctl) { 
                         case -1: aN = 0; bN = 0; break; 
                         

Real-world examples:
  - CVE-2019-15900: Chain: sscanf() call is used to check if a username and group exists, but the return value of sscanf() call is not checked (CWE-252), causing an uninitialized variable to be checked (CWE-457), returni
  - CVE-2008-3688: Chain: A denial of service may be caused by an uninitialized variable (CWE-457) allowing an infinite loop (CWE-835) resulting from a connection to an unresponsive server.
  - CVE-2008-0081: Uninitialized variable leads to code execution in popular desktop application.
  - CVE-2007-4682: Crafted input triggers dereference of an uninitialized object pointer.
  - CVE-2007-3468: Crafted audio file triggers crash when an uninitialized variable is used.