Before the lifetime of the last pointer object that stores the return value of a call to the fopen() or freopen() function has ended, the fclose() function must be called with that pointer value.
In general, this rule can also be applied to other functions with open and close resources, such as the POSIX open() and close() functions, or the Microsoft Windows CreateFile() and CloseHandle() functions.
This code example is noncompliant because the resource allocated by the call to fopen() is not closed before function func() returns.
#include <stdio.h>
int func(const char *filename) {
FILE *f = fopen(filename, "r");
if (NULL == f) {
return -1;
}
/* ... */
return 0;
} |
In this compliant solution, f is closed before returning to the caller:
#include <stdio.h>
int func(const char *filename) {
FILE *f = fopen(filename, "r");
if (NULL == f) {
return -1;
}
/* ... */
if (fclose(f) == EOF) {
return -1;
}
return 0;
} |
This code example is noncompliant because the resource allocated by the call to open() is not closed before function func() returns.
#include <stdio.h>
#include <fcntl.h>
int func(const char *filename) {
int fd = open(filename, O_RDONLY, S_IRUSR);
if (-1 == fd) {
return -1
}
/* ... */
return 0;
} |
In this compliant solution, fd is closed before returning to the caller:
#include <stdio.h>
#include <fcntl.h>
int func(const char *filename) {
int fd = open(filename, O_RDONLY, S_IRUSR);
if (-1 == fd) {
return -1
}
/* ... */
if (close(fd) == -1) {
return -1;
}
return 0;
} |
In this noncompliant code example, a file is opened using the Microsoft Windows CreateFile() API, but it is not subsequently closed before func() returns.
#include <Windows.h>
int func(LPCTSTR filename) {
HANDLE hFile = CreateFile(filename, GENERIC_READ, 0, NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE == hFile) {
return -1;
}
/* ... */
return 0;
} |
In this compliant solution, hFile is closed using the CloseHandle() API before returning to the caller.
#include <Windows.h>
int func(LPCTSTR filename) {
HANDLE hFile = CreateFile(filename, GENERIC_READ, 0, NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE == hFile) {
return -1;
}
/* ... */
if (CloseHandle(hFile) == 0) {
return -1;
}
return 0;
} |
Failing to properly close files may allow an attacker to exhaust system resources and increases the risk that data written into in-memory file buffers will not be flushed in the event of abnormal program termination. .
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
|---|---|---|---|---|---|
FIO42-C | Medium | Unlikely | Medium | P4 | L3 |
Tool | Version | Checker | Description |
|---|---|---|---|
5.0 | Can detect violations of this rule with CERT C Rule Pack | ||
RH.LEAK | |||
49 D | Fully implemented |
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
| CERT C++ Secure Coding Standard | FIO42-CPP. Ensure files are properly closed when they are no longer needed |
| CERT Oracle Secure Coding Standard for Java | FIO04-J. Release resources when they are no longer needed |
| ISO/IEC TS 17961 | Failing to close files or free dynamic memory when they are no longer needed [fileclose] |
| MITRE CWE | CWE-404, Improper resource shutdown or release |
| [IEEE Std 1003.1:2013] | XSH, System Interfaces, open |