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
C and C++
Binaries


IO.DC : Double Close

Summary

An attempt to close a file or socket that has already been closed.

Properties

Class Name Double Close
Significance reliability
Mnemonic IO.DC
Categories
MisraC2025 MisraC2025:D.4.1 Run-time failures shall be minimized
MisraC2023 MisraC2023:D.4.1 Run-time failures shall be minimized
Misra2012 Misra2012:D.4.1 Run-time failures shall be minimized
MisraC++2023 MisraC++2023:0.3.2 A function call shall not violate the function's preconditions
  MisraC++2023:15.8.1 User-provided copy assignment and move assignment operators shall handle self-assignment
CWE CWE:672 Operation on a Resource after Expiration or Release
  CWE:1341 Multiple Releases of Same Resource or Handle
CERT-CPP CERT-CPP:OOP54-CPP Gracefully handle self-copy assignment
JSF++ JSF++:81 The assignment operator shall handle self-assignment correctly.
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"

Example

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;
}

Note

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.

Triggering Functions

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.

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.

 

To report problems with this documentation, please visit https://support.codesecure.com/.