You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 9 Next »

According to [JLS Section 8.3.2.1, Initializers for Class Variables]:

"...at run time, static variables that are final and that are initialized with compile-time constant values are initialized first."

While this statement typically holds true, it can be misleading since it does not account for instances that use values of static final fields initialized at a later stage. Even if a field is static final, it is not necessarily initialized at first go.

Noncompliant Code Example

This noncompliant example contrives to calculate the account balance by subtracting the processing fee from the deposited amount, but fails miserably. The Cycle class object c is instantiated before the deposit field gets initialized. As a result, the constructor Cycle is invoked which computes the balance based on the initial value of deposit (0) rather than the random value. As a result, the balance always remains -10.

According to [JLS Section 12.4, Initialization of Classes and Interfaces]:

"Initialization of a class consists of executing its static initializers and the initializers for static fields (class variables) declared in the class."

This statement asserts that the presence of a static field triggers the initialization of a class, however, in this example, a recursive attempt is being made to initialize the class already. Since such recursive attempts are ignored by the JVM, the default value of deposit is 0 during the initialization. [[Bloch 05]]

public class Cycle {
  private static final Cycle c = new Cycle();
  private final int balance;
  private static final int deposit =  (int) (Math.random() * 100); //random deposit

  public Cycle(){
    balance = deposit - 10; //subtract processing fee
  }

  public static void main(String[] args) {
    System.out.println("The account balance is: " + c.balance);	
  }
}

Compliant Solution

This compliant solution changes the initialization order of the class Cycle so that the fields meant to be used in computations get duly initialized. As initialization cycles can become insidious when many classes are involved, proper care must be taken to inspect the control flow.

public class Cycle {
  private final int balance;
  private static final int deposit =  (int) (Math.random() * 100); //random deposit
  private static final Cycle c = new Cycle();  //inserted after initialization of required fields
  public Cycle(){
    balance = deposit - 10; //subtract processing fee
  }

  public static void main(String[] args) {
    System.out.println("The account balance is: " + c.balance);	
  }
}

Risk Assessment

Initialization cycles may lead to unexpected results.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

MSC00-J

low

unlikely

medium

P2

L3

Automated Detection

TODO

Related Vulnerabilities

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

References

[[JLS 05]] Sections 8.3.2.1, Initializers for Class Variables; 12.4, Initialization of Classes and Interfaces
[[Bloch 05]] Puzzle 49: Larger Than Life


EXC31-J. Handle checked exceptions that can be thrown within a finally block      99. The Void (VOID)      MSC00-J. Eliminate class initialization cycles

  • No labels