Static Analysis Problem Type Reference

File closed twice

A file was closed twice.

When a file handle is closed, resources associated with that file are released. Calling fclose twice will reference memory after it has been freed. This can have unpredictable consequences, especially if that storage has been reused for another purpose.

ID

Code Location

Description

1

Resource deallocation

The place where the file was closed the first time

2

Resource deallocation

The place where the file was closed the second time

Example


#include <stdio.h>

int main(int argc, char **argv)
{
    FILE *fp = fopen("newfile", "w");
    if (fp != 0) {
        fclose(fp);
        fclose(fp); // bad
    }
}