Skip to main content

IAR Embedded Workbench for RH850 3.20.x

RESOURCE-file-use-after-close

In this section:
Synopsis

A file resource is used after it has been closed.

Enabled by default

Yes

Severity/Certainty

High/Medium

highmedium.png
Full description

A file resource is referred to after it has been closed. When a file has been closed, any reference to it is invalid. Using this reference might cause an application crash. 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);
}