Skip to main content

IAR Embedded Workbench for RX 5.20

CERT-FIO46-C_b

In this section:
Synopsis

Do not access a closed file.

Enabled by default

Yes

Severity/Certainty

Medium/Low

mediumlow.png
Full description

Using the value of a pointer to a FILE object after the associated file is closed is undefined behavior. This check is identical to RESOURCE-file-use-after-close, SEC-FILEOP-use-after-close, MISRAC2012-Dir-4.13_f, CERT-FIO46-C_b.

Coding standards
CERT FIO46-C

Do not access a closed file

MISRA C:2012 Dir-4.13

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

Code examples

The following code example fails the check and will give a warning:

#include <stdio.h>

void example(void) {
  FILE *f1;
  f1 = fopen("test_file", "w");
  fclose(f1);
  fprintf(f1, "Hello, World!\n");
}

The following code example passes the check and will not give a warning about this issue:

#include <stdio.h>

void example(void) {
  FILE *f1;
  f1 = fopen("test_file", "w");
  fprintf(f1, "Hello, World!\n");
  fclose(f1);
}