Wiki Markup |
---|
According to \[§12.4, "Initialization of Classes and Interfaces"] of the _Java Language Specification_ \[[JLS 2005|AA. Bibliography#JLS 05]\], Section 12.4, "Initialization of Classes and Interfaces" |
Initialization of a class consists of executing its
static
initializers and the initializers forstatic
fields (class variables) declared in the class.
Wiki Markup |
---|
In other words, the presence of a {{static}} field triggers the initialization of a class. However, a static field mightcould depend on the initialization of a class, andpossibly might createcreating an initialization cycle. The _Java Language Specification_ also states: (\[[JLS 2005|AA. Bibliography#JLS 05]\], Section 8in §8.3.2.1, "Initializers for Class Variables") \[[JLS 2005|AA. Bibliography#JLS 05]\] |
...at run time,
static
variables that arefinal
and that are initialized with compile-time constant values are initialized first.
This statement can be misleading because it is inapplicable to instances that use values of static final
fields that are initialized at a later stage. Declaring a field to be static final
is insufficient to guarantee that it is fully initialized before being read.
Noncompliant Code Example (
...
Intra-
...
Class Cycle)
This noncompliant code example contains an intra-class initialization cycle.
...
The Cycle()
class declares a private static final
class variable which is initialized to a new instance of the Cycle()
class. Static initializers are guaranteed to be invoked once at some point before the first use of a static class member or the first invocation of a constructor.
The programmer's intent is to calculate the account balance by subtracting the processing fee from the deposited amount. However, the initialization of the c
class variable happens before the deposit
field is initialized because it appears lexically before the initialization of the deposit
field. Consequently, the value of deposit
seen by the constructor, when invoked during the static initialization of c
, is the initial value of deposit
(0) rather than the random value. As a result, the balance is always computed to be -10
.
Wiki Markup |
---|
The JLS _Java Language Specification_ permits implementations to ignore the possibility of such recursive attempts \[[Bloch 2005|AA. Bibliography#Bloch 05]\]. |
Compliant Solution (
...
Intra-
...
Class Cycle)
This compliant solution changes the initialization order of the class Cycle
so that the fields are initialized without creating any dependency cycles. Specifically, the initialization of c
is placed lexically after the initialization of deposit
so that it occurs temporally after deposit
is fully initialized.
...
Such initialization cycles become insidious when many fields are involved; ensure that the control flow lacks such cycles.
Noncompliant Code Example (
...
Inter-
...
Class Cycle)
This noncompliant code example declares two classes with static variables whose values depend on each other. The cycle is obvious when the classes are seen together (as like they are here), but this can easily be missed when the classes are viewed separately.
...
The initialization order of the classes can vary and, and consequently, cause computation of different values for A.a
and B.b
. When class A
is initialized first, A.a
will have the value 2, and B.b
will have the value 1. These values will be reversed when class B
is initialized first.
Compliant Solution (
...
Inter-
...
Class Cycle)
This compliant solution breaks the inter-class cycle by eliminating one of the dependencies.
...
Guideline | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
DCL12-J | low | unlikely | medium | P2 | L3 |
Automated Detection
TODO
Related
...
Guidelines
Search for vulnerabilities resulting from the violation of this guideline on the CERT website.
Other Languages
This guideline appears in the C++ Secure Coding Standard as : "DCL14-CPP. Avoid assumptions about the initialization order between translation units."
Bibliography
Wiki Markup |
---|
\[[JLS 2005|AA. Bibliography#JLS 05]\] Sections [8§8.3.2.1, Initializers for Class Variables|http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.3.2.1]; [12§12.4, Initialization of Classes and Interfaces|http://java.sun.com/docs/books/jls/third_edition/html/execution.html#12.4] Puzzle 49: Larger Than Life |
Wiki Markup |
---|
\[[MITRE 2009|AA. Bibliography#MITRE 09]\]
[CWE ID 665|http://cwe.mitre.org/data/definitions/665.html] "Improper Initialization" |
...