Main Content

MISRA C:2023 Rule 22.6

The value of a pointer to a FILE shall not be used after the associated stream has been closed

Since R2024a

Description

Rule Definition

The value of a pointer to a FILE shall not be used after the associated stream has been closed.

Rationale

The Standard states that the value of a FILE* pointer is indeterminate after you close the stream associated with it.

Polyspace Implementation

You can check for this rule with a Bug Finder analysis only.

Troubleshooting

If you expect a rule violation but do not see it, refer to Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

#include <stdio.h>

void func(void) {
    FILE *fp;
    void *ptr;

    fp = fopen("tmp","w");
    if(fp != NULL) {
        fclose(fp);
        fprintf(fp,"text"); // Non-compliant
    }
}

In this example, the stream associated with the FILE* pointer fp is closed with the fclose function. The rule is violated FILE* pointer fp is used before the stream is re-opened.

Check Information

Group: Resources
Category: Mandatory
AGC Category: Mandatory

Version History

Introduced in R2024a