Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This compliant solution declares the Helper helper field volatile.

Code Block
bgColor#ccccff
// Works with acquire/release semantics for volatile
// Broken under JDK 1.4 and earlier
final class Foo {
  private volatile Helper helper = null;

  public Helper getHelper() {
    if (helper == null) {
      synchronized (this) {
        if (helper == null) {
          helper = new Helper(); // If the helper is null, create a new instance
        }
      }
    }
    return helper; // If helper is non-null, return its instance
  }
}

...

Wiki Markup
\[[API 2006|AA. Java References#API 06]\]
\[[JLS 2005|AA. Java References#JLS 05]\] Section 12.4, "Initialization of Classes and Interfaces"
\[[Pugh 2004|AA. Java References#Pugh 04]\]
\[[Bloch 2001|AA. Java References#Bloch 01]\] Item 48: "Synchronize access to shared mutable data"
\[[Bloch 2008|AA. Java References#Bloch 08]\] Item 71: "Use lazy initialization judiciously"
\[[MITRE 2009|AA. Java References#MITRE 09]\] [CWE ID 609|http://cwe.mitre.org/data/definitions/609.html] "Double-Checked Locking"

Automated Detection

TODO

Related Vulnerabilities

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

...

THI06-J. Ensure that threads performing blocking operations can be terminated      12. Locking (LCK)