...
This noncompliant code example protects a resource using a ReentrantLock
but fails to release the lock if when an exception occurs while performing operations on the open file. When an exception is thrown, control transfers to the catch
block and the call to unlock()
fails to execute.
...
Note that the lock is still held, even when the doSomething()
method returns.
This noncompliant code example also fails to close the input stream and, consequently, violates rule FIO04-J. Close resources when they are no longer needed.
Compliant Solution (finally
Block)
This compliant solution encapsulates operations that could throw an exception in a try
block immediately after acquiring the lock. The lock is acquired just before the try
block, which guarantees that it is held when the finally
block executes. Invoking Lock.unlock()
in the finally
block ensures that the lock is released , regardless of whether an exception occurs.
...
The execute-around idiom provides a generic mechanism to perform resource allocation and clean-up cleanup operations so that the client can focus on specifying only the required functionality. This idiom reduces clutter in client code and provides a secure mechanism for resource management.
In this compliant solution, the client's doSomething()
method provides only the required functionality by implementing the doSomethingWithFile()
method of the LockAction
interface , without having to manage the acquisition and release of locks or the open and close operations of files. The ReentrantLockAction
class encapsulates all resource management actions.
Code Block | ||
---|---|---|
| ||
public interface LockAction { void doSomethingWithFile(InputStream in); } public final class ReentrantLockAction { public static void doSomething(File file, LockAction action) { Lock lock = new ReentrantLock(); InputStream in = null; lock.lock(); try { in = new FileInputStream(file); action.doSomethingWithFile(in); } catch (FileNotFoundException fnf) { // Forward to handler } finally { lock.unlock(); if (in != null) { try { in.close(); } catch (IOException e) { // Forward to handler } } } } } public final class Client { public void doSomething(File file) { ReentrantLockAction.doSomething(file, new LockAction() { public void doSomethingWithFile(InputStream in) { // Perform operations on the open file } }); } } |
Noncompliant Code Example (Unchecked Exception)
...
A runtime exception can occur because the doSomething()
method fails to check whether str
is a null reference, preventing the lock from being released.
...
Consequently, the lock is released even in the event of a runtime exception. The doSomething()
method also ensures avoids throwing a NullPointerException
by ensuring that the string is non-null to avoid throwing a NullPointerException
does not contain a null reference.
Risk Assessment
Failing Failure to release locks on exceptional conditions could lead to thread starvation and deadlock.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
LCK08-J | low | likely | low | P9 | L2 |
Related Vulnerabilities
The GERONIMO-2234 issue report describes a vulnerability in the Geronimo application server. If the user single-clicks the keystone portlet, the user will lock the default keystore without warning. This causes a crash and stack trace to be produced. However, the server cannot be restarted because the lock is never cleared.
Related Guidelines
Bibliography
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="a9811d02d6f92e33-ebd23236-49614de5-9334a25e-90c30538bfd196a45d38dd9e"><ac:plain-text-body><![CDATA[ | [[API 2006 | AA. Bibliography#API 06]] | Class | ]]></ac:plain-text-body></ac:structured-macro> |
...