Main Content

CWE Rule 672

Operation on a Resource after Expiration or Release

Since R2024a

Description

Rule Description

The product uses, accesses, or otherwise operates on a resource after that resource has been expired, released, or revoked.

Polyspace Implementation

The rule checker checks for these issues:

  • Closing previously closed resource

  • Use of previously closed resource

Examples

expand all

Issue

This issue occurs when a function attempts to close a stream that was closed earlier in your code and not reopened later.

Risk

The standard states that the value of a FILE* pointer is indeterminate after you close the stream associated with it. Performing the close operation on the FILE* pointer again can cause unwanted behavior.

Fix

Remove the redundant close operation.

Example — Closing Previously Closed Resource
#include <stdio.h>

void func(char* data) {
    FILE* fp = fopen("file.txt", "w");
    if(fp!=NULL) {
        if(data)
            fputc(*data,fp);
        else
            fclose(fp);
    }
    fclose(fp); //Noncompliant
}

In this example, if fp is not NULL and data is NULL, the fclose operation occurs on fp twice in succession.

Correction — Remove Close Operation

One possible correction is to remove the last fclose operation. To avoid a resource leak, you must also place an fclose operation in the if(data) block.

#include <stdio.h>

void func(char* data) {
    FILE* fp = fopen("file.txt", "w");
    if(fp!=NULL) {
        if(data) {
            fputc(*data,fp);
            fclose(fp);
        }
        else
            fclose(fp);
    }
}
Issue

This issue occurs when a function operates on a stream that you closed earlier in your code.

Risk

The standard states that the value of a FILE* pointer is indeterminate after you close the stream associated with it. Operations using the FILE* pointer can produce unintended results.

Fix

One possible fix is to close the stream only at the end of operations. Another fix is to reopen the stream before using it again.

Example — Use of FILE* Pointer After Closing Stream
#include <stdio.h>

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

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

In this example, fclose closes the stream associated with fp. When you use fprintf on fp after fclose, the Use of previously closed resource defect appears.

Correction — Close Stream After All Operations

One possible correction is to reverse the order of the fprintf and fclose operations.

#include <stdio.h>

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

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

Check Information

Category: Others

Version History

Introduced in R2024a