Skip to main content

IAR Embedded Workbench for Arm 9.70.x

RESOURCE-file-no-close-all

In this section:
Synopsis

A file pointer is never closed.

Enabled by default

Yes

Severity/Certainty

High/Medium

highmedium.png
Full description

One or more file pointers are never closed. To avoid failure caused by resource exhaustion, all file pointers obtained dynamically by means of Standard Library functions must be explicitly released. Releasing them as soon as possible reduces the risk that exhaustion will occur. This check is identical to MISRAC2012-Dir-4.13_c, MISRAC2012-Rule-22.1_b, SEC-FILEOP-open-no-close, 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 Dir-4.13

(Advisory) Functions which are designed to provide operations on a resource should be called in an appropriate sequence

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);
}