...
Code Block | ||
---|---|---|
| ||
private final Boolean initialized = Boolean.FALSE; synchronizedpublic void doSomething(initialized) { if synchronized(!initialized) { initialized = Boolean.TRUE;// ... } } |
Wiki Markup |
---|
There can only be two possible valid values ({{true}} and {{false}}, discounting {{null}}) that {{initialized}} can assume. Consequently, any other code that synchronizes on a {{Boolean}} variable with the same value, may induce unresponsiveness and deadlocks \[[Findbugs 08|AA. Java References#Findbugs 08]\]. |
...
Code Block | ||
---|---|---|
| ||
int lock = 0; final Integer Lock = lock; // Boxed primitive Lock will be shared public void doSomething() { synchronized(Lock) { /*/ ... */ } } |
Boxed types are allowed to use the same instance for a range of integer values and consequently, suffer from the same problems as Boolean
constants. If the primitive can be represented as a byte, the wrapper object is reused. 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.
...
Code Block | ||
---|---|---|
| ||
int lock = 0; private final Integer Lock = new Integer(lock); public void doSomething() { synchronized(Lock) { /*/ ... */ } } |
When explicitly constructed, an Integer
object has a unique reference and its own intrinsic lock that is not shared by other Integer
objects or boxed integers having the same value. While this is an acceptable solution, it may cause maintenance problems. It is always better to synchronize on a private final raw Object
as described next.
...
Code Block | ||
---|---|---|
| ||
private final Object lock = new Object(); public void doSomething() { synchronized(lock) { // ... } } |
For more information on using an Object
as a lock, see CON04-J. Synchronize using an internal private final lock object.
...
Code Block | ||
---|---|---|
| ||
private final String _lock = new String("LOCK").intern(); public void doSomething() { synchronized(_lock) { /*/ ... */ } } |
Wiki Markup |
---|
According to the Java API \[[API 06|AA. Java References#API 06]\], class {{String}} documentation: |
...
Code Block | ||
---|---|---|
| ||
// This bug was found in jetty-6.1.3 BoundedThreadPool private final String _lock = "LOCK"; // ... synchronized(_lock) { /*/ ... * } // }... |
A String
literal is a constant and is interned. Consequently, it suffers from the same pitfalls as the preceding noncompliant code example.
...
Code Block | ||
---|---|---|
| ||
private final String _lock = new String("LOCK"); public void doSomething() { synchronized(_lock) { //* ... */ } } |
A String
instance differs from a String
literal. The instance has a unique reference and its own intrinsic lock, not shared by other string objects or literals. A more suitable approach is to use the private final internal raw Object
discussed earlier.
...
Synchronizing on return values of the Object.getClass()
method, rather than a class literal can also be counterproductive. Whenever the implementing class is subclassed, the subclass locks on a completely different Class
object (subclass's type).
Code Block | ||
---|---|---|
| ||
public void doSomething() { synchronized(getClass()) { //* ... */ } } |
Wiki Markup |
---|
Section 4.3.2 "The Class Object" of the Java Language specification \[[JLS 05|AA. Java References#JLS 05]\] describes how method synchronization works: |
...
Explicitly define the name of the class through name qualification (superclass in this compliant solution) in the synchronization block.
Code Block | ||
---|---|---|
| ||
public void doSomething() { synchronized(SuperclassName.class) { // ... } } |
The class object being synchronized must not be accessible to hostile code. If the class is package-private, then external packages may not access the Class object, ensuring its trustworthiness as an intrinsic lock object. For more information, see CON04-J. Synchronize using an internal private final lock object.
...
This compliant solution uses the Class.forName()
method to synchronize on the superclass's Class
object.
Code Block | ||
---|---|---|
| ||
public void doSomething() { synchronized(Class.forName("SuperclassName")) { // ... } } |
Again, the class object being synchronized must not be accessible to hostile code, as discussed in the previous compliant solution. Furthermore, care must be taken so that untrusted inputs are not accepted as arguments while loading classes using Class.forname()
(see SEC05-J. Do not expose standard APIs that use the immediate caller's class loader instance to untrusted code for more information).
...
Code Block | ||
---|---|---|
| ||
final Lock lock = new ReentrantLock(); public void doSomething() { synchronized(lock) { //* ... */ } } |
Similarly, it is inappropriate to lock on an object of a class that implements either the Lock
or Condition
interface (or both) of package java.util.concurrent.locks
. This problem usually comes up in practice when refactoring from intrinsic locking to the java.util.concurrent
dynamic locking utilities.
...
Code Block | ||
---|---|---|
| ||
private final Map<Integer, String> map = Collections.synchronizedMap(new HashMap<Integer, String>()); private final Set<Integer> set = map.keySet(); public void doSomething() { synchronized(set) { // Incorrectly synchronizes on set for(Integer k : set) { // Do something ... } } } |
Wiki Markup |
---|
When using synchronization wrappers, the synchronization object must be the {{Collection}} object. The synchronization is necessary to enforce atomicity ([CON07-J. Do not assume that a grouping of calls to independently atomic methods is atomic]). This noncompliant code example demonstrates inappropriate synchronization resulting from locking on a Collection view instead of the Collection object itself \[[Tutorials 08|AA. Java References#Tutorials 08]\]. |
...
Code Block | ||
---|---|---|
| ||
// ... Map<Integer, String> map = Collections.synchronizedMap(new HashMap<Integer, String>()); public void doSomething() { synchronized(map) { // Synchronize on map, not set for(Integer k : map) { // Do something } } } |
Finally, it is more important to recognize the entities with whom synchronization is required rather than indiscreetly scavenging for variables or objects to synchronize on.
...
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
CON02- J | medium | probable | medium | P8 | L2 |
Automated Detection
...
Currently, SureLogic Flashlight does not detect all violations of this guideline. It detects:
Noncompliant Code Example | Message |
---|
It does not detect:
Noncompliant Code Example | Message |
---|---|
| No obvious issues |
Boxed primitive | No obvious issues |
interned | No obvious issues |
String literal | No data available about field accesses |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
...