...
A program may leak resources when it relies on finalize()
to release system resources or when there is confusion over which part of the program is responsible for releasing system resources. In a busy system, the delay before the finalize()
method is called for an object provides a window of vulnerability during which an attacker could induce a denial-of-service attack. Consequently, resources other than raw memory must be explicitly freed in non-finalizer methods , because of the unsuitability of using finalizers. See the rule MET12-J. Do not use finalizers for additional reasons to avoid the use of finalizers.
Note that on the Windows platform, attempts to delete open files fail silently. See rule FIO03-J. Remove temporary files before termination for more information.
...
This noncompliant code example opens a file and uses it , but fails to explicitly close the file.
...
Code Block | ||
---|---|---|
| ||
try { final FileInputStream stream = new FileInputStream(fileName); try { final BufferedReader bufRead = new BufferedReader(new InputStreamReader(stream)); String line; while ((line = bufRead.readLine()) != null) { sendLine(line); } } finally { if (stream != null) { try { stream.close(); } catch (IOException e) { // forward to handler } } } } catch (IOException e) { // forward to handler } |
Compliant Solution (Java 1.7, try-with-resources)
...
Code Block | ||
---|---|---|
| ||
try (FileInputStream stream = new FileInputStream(fileName); BufferedReader bufRead = new BufferedReader(new InputStreamReader(stream))) { String line; while ((line = bufRead.readLine()) != null) { sendLine(line); } } catch (IOException e) { // forward to handler } |
The try-with-resource resources construct sends any IOException
to the catch
clause, where it is forwarded to an exception handler. This includes exceptions generated during the allocation of resources (that is, the creation of the FileInputStream
or BufferedReader
). It also includes any IOException
thrown during the while loop. Finally, it includes any IOException
generated by closing bufRead
or stream
.
...
This noncompliant code example attempts to address exhaustion of database connections by adding clean-up code in a finally
block. However, either or both of rs
and stmt
could be null
, causing the code in the finally
block to throw a NullPointerException
.
Code Block | ||
---|---|---|
| ||
Statement stmt = null; ResultSet rs = null Connection conn = getConnection(); try { stmt = conn.createStatement(); rs = stmt.executeQuery(sqlQuery); processResults(rs); } catch(SQLException e) { // forward to handler } finally { rs.close(); stmt.close(); } |
...
Code Block | ||
---|---|---|
| ||
Statement stmt = null; ResultSet rs = null; Connection conn = getConnection(); try { stmt = conn.createStatement(); rs = stmt.executeQuery(sqlQuery); processResults(rs); } catch (SQLException e) { // forward to handler } finally { if (rs != null) { rs.close(); } if (stmt != null) { stmt.close(); } } |
...
Code Block | ||
---|---|---|
| ||
Statement stmt = null; ResultSet rs = null; Connection conn = getConnection(); try { stmt = conn.createStatement(); rs = stmt.executeQuery(sqlQuery); processResults(rs); } catch (SQLException e) { // forward to handler } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) { // forward to handler } finally { if (stmt != null) { try { stmt.close(); } catch (SQLException e) { // forward to handler } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { // forward to handler } } } } } } |
Compliant Solution (Java 1.7, try-with-resources)
This compliant solution uses the try-with-resource resources construct, introduced in Java 1.7, to ensure that resources are released as required.
Code Block | ||
---|---|---|
| ||
try (Connection conn = getConnection(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sqlQuery)) { processResults(rs); } catch (SQLException e) { // forward to handler } |
The try-with-resource resources construct sends any SQLException
to the catch
clause, where it gets forwarded to an exception handler. This includes exceptions generated during the allocation of resources (that is, the creation of the Connection
, Statement
, or ResultSet
). It also includes any SQLException
thrown by processResults()
. Finally, it includes any SQLException
generated by closing rs
, stmt
, or conn
.
...
FIO42-C. Ensure files are properly closed when they are no longer needed | |
FIO42-CPP. Ensure files are properly closed when they are no longer needed | |
CWE-404 "Improper Resource Shutdown or Release" | |
| CWE-459 "Incomplete Cleanup" |
| CWE-770 "Allocation of Resources Without Limits or Throttling" |
| CWE-405 "Asymmetric Resource Consumption (Amplification)" |
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="a1ad4f5c707e0078-04503d04-43614d14-b2fb8a3e-72a3e92d53071d53ae354583"><ac:plain-text-body><![CDATA[ | [[API 2006 | AA. Bibliography#API 06]] | [Class Object | http://java.sun.com/javase/6/docs/api/java/lang/Object.html] | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="dd3df8c99cb4e67e-f8debf67-4b054b93-932cafbe-f67837d45dacaa3d1af0e5a0"><ac:plain-text-body><![CDATA[ | [[Goetz 2006b | AA. Bibliography#Goetz 06b]] |
| ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="4f2583352a012cc7-95c67305-42ff46d1-b6c89306-5cb75b040ddaa6dc2240f388"><ac:plain-text-body><![CDATA[ | [[J2SE 2011 | AA. Bibliography#J2SE 11]] | The try-with-resources Statement | ]]></ac:plain-text-body></ac:structured-macro> |
...