JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.
If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.
If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.
| CodeSonar® 9.2p0 | CONFIDENTIAL | CodeSecure Inc |
An attempt to close a file or socket that has already been closed.
| Class Name | Double Close | |||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Significance | reliability | |||||||||||||||||||||||||||
| Mnemonic | IO.DC | |||||||||||||||||||||||||||
| Categories |
|
|||||||||||||||||||||||||||
| Availability | Available for C and C++. |
|||||||||||||||||||||||||||
| Enabling | Checks for this warning class are enabled by
default. To disable them, add the following WARNING_FILTER rule to the
project configuration file.
WARNING_FILTER += discard class="Double Close" |
Calling close() (or a similar function) twice with the same file descriptor can have unexpected negative consequences if the system has recycled the descriptor associated with the closed file:
#include <fcntl.h>
#include <unistd.h>
int double_close(const void *databuf, size_t datasize){
int mydesc;
int yourdesc;
ssize_t s;
int rv;
mydesc = open("myfile.txt", O_CREAT|O_RDONLY);
if (mydesc < 0){return -1;}
close(mydesc);
/* system may recycle the descriptor of the closed file
* giving yourdesc == mydesc */
yourdesc = open("yourfile.txt", O_CREAT|O_RDWR);
if (yourdesc < 0){return -2;}
/* if yourdesc == mydesc, equivalent to close(yourdesc) */
close(mydesc); /* 'Double Close' warning issued here */
/* if yourdesc == mydesc, may fail unexpectedly */
s = write(yourdesc, databuf, datasize);
close(yourdesc);
if (s < 0){return -3;}
return 0;
}
Calling fclose() (or a similar function) twice with the same file pointer causes problems similar to those caused by freeing a pointer twice: undefined and unpredictable behavior including possible memory corruption.
CodeSonar ships with library models that allow it to recognize functions such as libc close() and Win32 FlsFree() that close a file or socket. If one of these functions is called on a file or socket that is already closed, a warning will be issued.
If you have created a custom library model for some function f() in terms of one of these existing models, calls to f() will also be capable of triggering Double Close warnings.
The following configuration file parameters affect checks for this warning class.
To report problems with this documentation, please visit https://support.codesecure.com/.