Missing Initialization of a Variable

The software does not initialize critical variables, which causes the execution environment to use unexpected values.


Demonstrations

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 function attempts to extract a pair of numbers from a user-supplied string.

void parse_data(char *untrusted_input){

  int m, n, error;
  error = sscanf(untrusted_input, "%d:%d", &m, &n);
  if ( EOF == error ){
    die("Did not specify integer value. Die evil hacker!\n");
  }
  /* proceed assuming n and m are initialized correctly */


}

This code attempts to extract two integer values out of a formatted, user-supplied input. However, if an attacker were to provide an input of the form:

123:

then only the m variable will be initialized. Subsequent use of n may result in the use of an uninitialized variable (CWE-457).

Example Two

Here, an uninitialized field in a Java class is used in a seldom-called method, which would cause a NullPointerException to be thrown.

private User user;
public void someMethod() {


  // Do something interesting.
  ...

  // Throws NPE if user hasn't been properly initialized.
  String username = user.getName();

}

Example Three

This code first authenticates a user, then allows a delete command if the user is an administrator.

if (authenticate($username,$password) && setAdmin($username)){
  $isAdmin = true;
}
/.../

if ($isAdmin){
  deleteUser($userToDelete);
}

The $isAdmin variable is set to true if the user is an admin, but is uninitialized otherwise. If PHP's register_globals feature is enabled, an attacker can set uninitialized variables like $isAdmin to arbitrary values, in this case gaining administrator privileges by setting $isAdmin to true.

Example Four

In the following Java code the BankManager class uses the user variable of the class User to allow authorized users to perform bank manager tasks. The user variable is initialized within the method setUser that retrieves the User from the User database. The user is then authenticated as unauthorized user through the method authenticateUser.

public class BankManager {



  // user allowed to perform bank manager tasks
  private User user = null;
  private boolean isUserAuthentic = false;

  // constructor for BankManager class
  public BankManager() {
    ...
  }

  // retrieve user from database of users
  public User getUserFromUserDatabase(String username){
    ...
  }

  // set user variable using username
  public void setUser(String username) {
    this.user = getUserFromUserDatabase(username);
  }

  // authenticate user
  public boolean authenticateUser(String username, String password) {
    if (username.equals(user.getUsername()) && password.equals(user.getPassword())) {
      isUserAuthentic = true;
    }
    return isUserAuthentic;
  }

  // methods for performing bank manager tasks
  ...

}

However, if the method setUser is not called before authenticateUser then the user variable will not have been initialized and will result in a NullPointerException. The code should verify that the user variable has been initialized before it is used, as in the following code.

public class BankManager {



  // user allowed to perform bank manager tasks
  private User user = null;
  private boolean isUserAuthentic = false;

  // constructor for BankManager class
  public BankManager(String username) {
    user = getUserFromUserDatabase(username);
  }

  // retrieve user from database of users
  public User getUserFromUserDatabase(String username) {...}

  // authenticate user
  public boolean authenticateUser(String username, String password) {

    if (user == null) {
      System.out.println("Cannot find user " + username);
    }
    else {
      if (password.equals(user.getPassword())) {
        isUserAuthentic = true;
      }
    }
    return isUserAuthentic;

  }



    // methods for performing bank manager tasks
    ...




}

See Also

SEI CERT C Coding Standard - Guidelines 12. Error Handling (ERR)

Weaknesses in this category are related to the rules and recommendations in the Error Handling (ERR) section of the SEI CERT C Coding Standard.

CISQ Quality Measures - Security

Weaknesses in this category are related to the CISQ Quality Measures for Security. Presence of these weaknesses could reduce the security of the software.

CISQ Quality Measures - Reliability

Weaknesses in this category are related to the CISQ Quality Measures for Reliability. Presence of these weaknesses could reduce the reliability of the software.

Comprehensive CWE Dictionary

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

CWE Cross-section

This view contains a selection of weaknesses that represent the variety of weaknesses that are captured in CWE, at a level of abstraction that is likely to be useful t...

Weaknesses Introduced During Implementation

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


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.