983
processLine(line);
}
fil.Close();
}
Example 2:
This code attempts to open a connection to a database and catches any exceptions that may
occur.
Example Language: Java (Bad)
try {
Connection con = DriverManager.getConnection(some_connection_string);
}
catch ( Exception e ) {
log( e );
}
If an exception occurs after establishing the database connection and before the same connection
closes, the pool of database connections may become exhausted. If the number of available
connections is exceeded, other users cannot access this resource, effectively denying access to
the application.
Example 3:
Under normal conditions the following C# code executes a database query, processes the results
returned by the database, and closes the allocated SqlConnection object. But if an exception
occurs while executing the SQL or processing the results, the SqlConnection object is not closed.
If this happens often enough, the database will run out of available cursors and not be able to
execute any more SQL queries.
Example Language: C# (Bad)
...
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(queryString);
cmd.Connection = conn;
conn.Open();
SqlDataReader rdr = cmd.ExecuteReader();
HarvestResults(rdr);
conn.Connection.Close();
...
Example 4:
The following C function does not close the file handle it opens if an error occurs. If the process is
long-lived, the process can run out of file handles.
Example Language: C (Bad)
int decodeFile(char* fName) {
char buf[BUF_SZ];
FILE* f = fopen(fName, "r");
if (!f) {
printf("cannot open %s\n", fName);
return DECODE_FAIL;
}
else {
while (fgets(buf, BUF_SZ, f)) {
if (!checkChecksum(buf)) {
return DECODE_FAIL;
}
else {
decodeBlock(buf);
CWE Version 4.14
CWE-404: Improper Resource Shutdown or Release
CWE-404: Improper Resource Shutdown or Release