Skip to main content

IAR Embedded Workbench for RL78 5.20

SEC-FILEOP-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. Once a file has been closed, the reference to that file is invalidated. Any use of this reference is undefined and might result in an application crash. A file pointer should not be used after the file it points to is closed. To use the file pointer again, you must open a new file with that pointer. 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);
}