Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-3588

Closed resources should not be accessed

    XMLWordPrintable

    Details

    • Message:
      File "xxx" has already been closed.
    • Highlighting:
      Hide

      primary: FILE access
      secondary: where the FILE was closed

      Show
      primary: FILE access secondary: where the FILE was closed
    • Default Severity:
      Blocker
    • Impact:
      High
    • Likelihood:
      High
    • Default Quality Profiles:
      Sonar way, MISRA C++ 2008 recommended
    • Covered Languages:
      C, C++, Objective-C
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      15min
    • CERT:
      FIO46-C.

      Description

      Using the value of a pointer to a FILE object after the associated file is closed is undefined behavior.

      Noncompliant Code Example

      void fun() {
        FILE * pFile;
        pFile = fopen(fileName, "w");
      
        if (condition) {
          fclose(pFile);
          // ...
        }
      
        fclose(pFile); // Noncompliant, the file has already been closed
      }
      

      Compliant Solution

      void fun() {
        FILE * pFile;
        pFile = fopen(fileName, "w");
      
        if (condition) {
          // ...
        }
      
        fclose(pFile);
      }
      

      See

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              massimo.paladin Massimo PALADIN
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated: