CWE Rule 672
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
Closing previously closed resource
This issue occurs when a function attempts to close a stream that was closed earlier in your code and not reopened later.
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.
Remove the redundant close operation.
#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.
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); } }
Use of previously closed resource
This issue occurs when a function operates on a stream that you closed earlier in your code.
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.
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.
#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.
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
See Also
External Websites
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)