Menu

#1471 [java] DoubleCheckedLocking: False positives

PMD-5.3.7
closed
java (55)
PMD
3-Major
Bug
5.3.x
DoubleCheckedLocking
2016-06-25
2016-04-19
Alix Warnke
No

The rule triggers on the following implementation (in a Java7 codebase):

private static volatile Foo instance;

public static Foo getInstance() {
    Foo result = instance;
    if (result == null) {
        synchronized (Foo.class) {
            result = instance;
            if (result == null) {
                result = instance = new Foo();
            }
        }
    }
    return result;
}

This implementation should be OK for JDK 5+ according to: http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html (section "Under the new Java Memory Model")

Discussion

  • Andreas Dangel

    Andreas Dangel - 2016-04-23
     
  • Andreas Dangel

    Andreas Dangel - 2016-04-23
    • status: open --> closed
    • assigned_to: Andreas Dangel
    • Milestone: New Tickets --> PMD-5.3.7
    • Affects version: 5.4.0 --> 5.3.x
     
  • Andreas Dangel

    Andreas Dangel - 2016-04-23

    As a workaround, you can get rid of the false positive by avoiding to use a local variable ("result"), until the next pmd release.

    This bug will be fixed with PMD 5.3.7, 5.4.2 and 5.5.0.

    Commit: https://github.com/pmd/pmd/commit/cb5ac3086e4d33c50c1bb91ca0a9971429327bd7

     

    Last edit: Andreas Dangel 2016-04-30
  • Andreas Dangel

    Andreas Dangel - 2016-06-25
    • labels: --> java
    • summary: False positives for DoubleCheckedLocking --> [java] DoubleCheckedLocking: False positives
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.