CWE-404¶
Improper Resource Shutdown or Release. [Improper-Control-Of-A-Resource-Through-Its-Lifetime]
Required inputs: IR, StaticSemanticAnalysis
Demonstrative Examples
Example 1
The following method never closes the new file handle. Given enough time, the Finalize() method for BufferReader should eventually call Close(), but there is no guarantee as to how long this action will take. In fact, there is no guarantee that Finalize() will ever be invoked. In a busy environment, the Operating System could use up all of the available file handles before the Close() function is called.
Example Language:Java (Unsupported language for documentation only)
private void processFile(string fName)
{
BufferReader fil = new BufferReader(new FileReader(fName));
String line;
while ((line = fil.ReadLine()) != null)
{
processLine(line);
}
}
The good code example simply adds an explicit call to the Close() function when the system is done using the file. Within a simple example such as this the problem is easy to see and fix. In a real system, the problem may be considerably more obscure.
Example Language:Java (Unsupported language for documentation only)
private void processFile(string fName)
{
BufferReader fil = new BufferReader(new FileReader(fName));
String line;
while ((line = fil.ReadLine()) != null)
{
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 (Unsupported language for documentation only)
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# (Unsupported language for documentation only)
...
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
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);
}
}
}
fclose(f);
return DECODE_SUCCESS;
}
Example 5
In this example, the program does not use matching functions such as malloc/free, new/delete, and new[]/delete[] to allocate/deallocate the resource.
Example Language:C++
class A {
void foo();
};
void A::foo(){
int *ptr;
ptr = (int*)malloc(sizeof(int));
delete ptr;
}
Example 6
In this example, the program calls the delete[] function on non-heap memory.
Example Language:C++
class A{
void foo(bool);
};
void A::foo(bool heap) {
int localArray[2] = {
11,22
};
int *p = localArray;
if (heap){
p = new int[2];
}
delete[] p;
}Excerpts from CWE [https://cwe.mitre.org], Copyright (C) 2006-2026, the MITRE Corporation. See section 9.4. "3rd-Party Licenses" in the documentation for full details.Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
memory_leak |
Call allocates leaking memory |
None |
False |
possible_memory_leak |
Call allocates possibly leaking memory |
None |
False |
possible_stack_free |
{name0} possibly released by call to {node0} is a stack or static object |
None |
False |
possible_wrong_release |
Resource possibly released using wrong function (allocation used {node0}) |
None |
False |
stack_free |
{name0} released by call to {node0} is a stack or static object |
None |
False |
wrong_close_function |
Trying to close {} with ‘{}’. |
None |
False |
wrong_release |
Resource released using wrong function (allocation used {node0}) |
None |
False |
Options¶
This rule shares the following common options: exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
resources¶
resources : set[str] = {'C++ArrayHeapMemory', 'C++HeapMemory', 'FileHandle', 'HeapMemory'}
witness_paths¶
witness_paths : bool = True
witness_should_include_exception_handling¶
witness_should_include_exception_handling : bool = False