If the program relies on finalize()
to release system resources, or there is confusion over which part of the program is responsible for releasing system resources, then there exists a possibility for a potential resource leak. In a busy system, there might be a time gap before the finalize()
method is called for an object. An attacker might exploit this vulnerability to induce a Denial of Service attack. Rule OBJ02-J has more information on the correct usage of finalizers.
If there is unreleased memory, eventually the Java garbage collector will be called to free memory; however, if the program relies on non-memory resources like file descriptors and database connections, unreleased resources might lead the program to prematurely exhaust it's pool of resources. In addition, if the program uses resources like Lock
or Semaphore
, waiting for finalize()
to release the resources may lead to resource starvation.
...
Code Block | ||
---|---|---|
| ||
Statement stmt = null;
ResultSet rs = null;
Connection conn = getConnection();
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sqlQuery);
processResults(rs);
} catch(SQLException e) { }
finally {
try {
if(rs != null) {
rs.close();
}
} finally (
try {
if(stmt != null) {
stmt.close();
}
}
finally {
conn.close();
}
}
}
|
Noncompliant Code Example
...