Skip to main content

IAR Embedded Workbench for Arm 9.70.x

MISRAC2012-Dir-4.13_f

In this section:
Synopsis

(Advisory) Functions which are designed to provide operations on a resource should be called in an appropriate sequence. 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.

Enabled by default

No

Severity/Certainty

High/Medium

highmedium.png
Full description

A file resource is used after it has been closed.

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