SEC-FILEOP-open-no-close
Synopsis
All file pointers obtained dynamically by means of Standard Library functions must be explicitly released.
Enabled by default
Yes
Severity/Certainty
High/Medium

Full description
If file pointers are not explicitly released, a failure might occur caused by exhaustion of the resources. Release file pointers as soon as possible to reduce the risk of exhaustion. Make sure that files are closed on all execution paths in a function. This check is identical to MISRAC2012-Dir-4.13_c, MISRAC2012-Rule-22.1_b, RESOURCE-file-no-close-all, CERT-FIO42-C_a.
Coding standards
- CERT FIO42-C
Ensure files are properly closed when they are no longer needed
- CWE 404
Improper Resource Shutdown or Release
- MISRA C:2012 Rule-22.1
(Required) All resources obtained dynamically by means of Standard Library functions shall be explicitly released
Code examples
The following code example fails the check and will give a warning:
#include <stdio.h>
void example(void) {
FILE *fp = fopen("test.txt", "c");
}
The following code example passes the check and will not give a warning about this issue:
#include <stdio.h>
void example(void) {
FILE *fp = fopen("test.txt", "c");
fclose(fp);
}