Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: general edit

...

Noncompliant Code Example (String constant)

This noncompliant code example locks on a String literal.

Code Block
bgColor#FFcccc

// This bug was found in jetty-6.1.3 BoundedThreadPool
private final String _lock = "one";
synchronized(_lock) { /* ... */ }

Wiki Markup
A {{String}} literal is a constant and is interned in Java. According to the Java API \[[API 06|AA. Java References#API 06]\] Class {{String}} documentation:

...

Consequently, a String constant behaves like a global variable in the JVM. As demonstrated in this noncompliant code example, even if each instance of an object maintains its own field lock, it points to a common String constant in the JVM. Legitimate Trusted code that locks on the same String constant renders all synchronization attempts inadequate. Likewise, hostile code from any other package can deliberately exploit this vulnerability.

...

bgColor#FFcccc

...

.

...

Noncompliant Code Example (Boxed primitive)

...

Code Block
bgColor#FFcccc
int lock = 0;
Integer Lock = lock; // Boxed primitive Lock will be shared
synchronized(Lock) { /* ... */ }

Boxed types may are allowed to use the same instance for a range of integer values and consequently, suffer from the same problems as String constants. Note that the boxed Integer primitive is shared and not the Integer object (new Integer(value)) itself. In general, holding a lock on any data structure that contains a boxed value is insecure.

...

This is because the thread that holds a lock on the nonfinal field object can modify the field's value, allowing another thread that is blocked on the unmodified value to resume, at the same time, contending for the lock with a third thread that is blocked on the modified value. It is insecure to synchronize on a mutable field because this is equivalent to synchronizing on the field's contents. This is a mutual exclusion problem as opposed to the sharing issue discussed in the previous noncompliant code examplespecific to string literals and boxed primitives.

Compliant Solution (final lock object)

...

Wiki Markup
This noncompliant code example uses a {{Boolean}} field to synchronize. However, there can only be two possible valid values ({{true}} and {{false}}, discounting {{null}}) that a {{Boolean}} can assume. Consequently, any other code that synchronizes on the same value can cause unresponsiveness and deadlocks \[[Findbugs 08|AA. Java References#Findbugs 08]\].

...

This does not mean that a subclass can only synchronize on the Class object of the base class and can safely use getClass().

Compliant Solution (1) (class name qualification)

...

This noncompliant code example uses a nonstatic lock object to guard access to a static field. If two Runnable tasks, each consisting of a thread are started, they will create two instances of the lock object and lock on each separately. This does not prevent either thread from observing an inconsistent value of field counter because the increment operation on volatile fields is not atomic , in the absence of proper synchronization.

...

Code Block
bgColor#ccccff
class CountBoxes implements Runnable {
  static volatile int counter;
  // ...

  static final Object lock = new Object();    
  // ...
}

...