Protecting the fields of a class is unnecessary when it is designed for single-threaded use. Such classes are required to document their lack of thread-safety. For instance, the documentation of class java.lang.StringBuilder
states [[API 06]] :
This class is designed for use as a drop-in replacement for
StringBuffer
in places where the string buffer was being used by a single thread (as is generally the case). Where possible, it is recommended that this class be used in preference toStringBuffer
as it will be faster under most implementations.
Multithreaded clients of classes that are not thread-safe must protect any accesses if the documentation of the classes specify the lack of thread-safety, or fail to provide any conclusive information.
Classes that use static
fields that are both publicly accessible and mutable must always protect accesses to their fields, regardless of their documentation. This is because there is no guarantee that all clients will synchronize externally when accessing the field. Because a static
field is shared by all clients, unrelated clients may violate the contract by not performing suitable locking.
Noncompliant Code Example
This noncompliant code example does not synchronize access to the static
field counter
.
/** This class is not thread-safe! */ public final class CountHits { private static int counter; public void incrementCounter() { counter++; } }
This class is not thread-safe because it violates [CON01-J. Ensure that compound operations on shared variables are atomic].
The class relies on clients to externally synchronize the object and this class documents its lack of thread-safety. This code is non-compliant, not because it violates [CON01-J. Ensure that compound operations on shared variables are atomic], and not because it documents its lack of thread-safety, but rather because it has an accessible mutable static field counter
. Untrusted code is free to modify CountHits.counter
with no respect for this class's documented lack of thread-safety.
Compliant Solution
This compliant solution uses a static private final lock to protect the counter
field and consequently, does not depend on any external synchronization. This is recommended by [CON04-J. Use private final lock objects to synchronize classes that may interact with untrusted code].
/** This class is thread-safe */ public final class CountHits { private static int counter; private static final Object lock = new Object(); public void incrementCounter() { synchronized (lock) { counter++; } } }
Risk Assessment
Failing to protect classes containing accessible static members can result in unexpected results when a client fails to obey the classes' synchronization policy.
Rule |
Severity |
Likelihood |
Remediation Cost |
Priority |
Level |
---|---|---|---|---|---|
CON32- J |
low |
probable |
medium |
P4 |
L3 |
Issue Tracking
Review List
Automated Detection
TODO
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
References
[[API 06]]
[[Bloch 08]] Item 67: "Avoid excessive synchronization"
Issue Tracking
Review List
[!The CERT Sun Microsystems Secure Coding Standard for Java^button_arrow_left.png!] [!The CERT Sun Microsystems Secure Coding Standard for Java^button_arrow_up.png!] null