Static Analysis Problem Type Reference
A file handle was used that isn't a valid open file.
This error usually indicates that a file handle was used after it was closed. An attempt to use a file handle that was never opened would be reported as an uninitialized variable error rather than an invalid file handle error. A file open operation that fails returns a null pointer. An attempt to use a file handle without checking that the open succeeded would be reported as a possible null pointer dereference error rather than an invalid file handle error.
When a file handle is closed, resources associated with that file are released. Using that file handle after it is closed can cause a reference to memory that has been freed. This can have unpredictable consequences, especially if that storage has been reused for another purpose.
|
ID |
Code Location |
Description |
|---|---|---|
|
1 |
Use of invalid resource |
The place where the invalid file handle was used |
#include <stdio.h>
int main(int argc, char **argv)
{
FILE *fp;
int data;
fp = fopen("newfile", "w");
if (fp != 0) {
fclose(fp);
data = fgetc(fp); // bad - file already closed
}
}