CertC-FIO24

Do not open a file that is already open

Required inputs: IR

Opening a file that is already open has implementation-defined behavior , according to the C Standard, 7.21.3, paragraph 8 [ ISO/IEC 9899:2011 ]:

Functions that open additional (nontemporary) files require a file name, which is a string. The rules for composing valid file names are implementation-defined. Whether the same file can be simultaneously open multiple times is also implementation-defined.

Some implementations do not allow multiple copies of the same file to be open at the same time. Consequently, portable code cannot depend on what will happen if this rule is violated. Even on implementations that do not outright fail to open an already-opened file, a TOCTOU (time-of-check, time-of-use) race condition exists in which the second open could operate on a different file from the first due to the file being moved or deleted (see FIO45-C. Avoid TOCTOU race conditions while accessing files for more details on TOCTOU race conditions).

Noncompliant Code Example

This noncompliant code example logs the program's state at runtime:

#include <stdio.h>

void do_stuff(void) {
  FILE *logfile = fopen("log", "a");
  if (logfile == NULL) {
    /* Handle error */
  }

  /* Write logs pertaining to do_stuff() */
  fprintf(logfile, "do_stuff\n");
}

int main(void) {
  FILE *logfile = fopen("log", "a");
  if (logfile == NULL) {
    /* Handle error */
  }

  /* Write logs pertaining to main() */
  fprintf(logfile, "main\n");

  do_stuff();

  if (fclose(logfile) == EOF) {
    /* Handle error */
  }
  return 0;
}

Because the file log is opened twice (once in main() and again in do_stuff() ), this program has implementation-defined behavior .

Compliant Solution

In this compliant solution, a reference to the file pointer is passed as an argument to functions that need to perform operations on that file. This reference eliminates the need to open the same file multiple times.

#include <stdio.h>

void do_stuff(FILE *logfile) {
  /* Write logs pertaining to do_stuff() */
  fprintf(logfile, "do_stuff\n");
}

int main(void) {
  FILE *logfile = fopen("log", "a");
  if (logfile == NULL) {
    /* Handle error */
  }

  /* Write logs pertaining to main() */
  fprintf(logfile, "main\n");

  do_stuff(logfile);

  if (fclose(logfile) == EOF) {
    /* Handle error */
  }
  return 0;
}
Risk Assessment

Simultaneously opening a file multiple times can result in unexpected errors and nonportable behavior.

Rule Severity Likelihood Detectable Repairable Priority Level
FIO24-C Medium Probable No No P4 L3
Related Guidelines
CERT C Secure Coding Standard FIO45-C. Avoid TOCTOU race conditions while accessing files
SEI CERT C++ Coding Standard VOID FIO21-CPP. Do not simultaneously open the same file multiple times
MITRE CWE CWE-362 , Concurrent Execution Using Shared Resource with Improper Synchronization ("Race Condition")
CWE-675 , Duplicate Operations on Resource
Bibliography
[ ISO/IEC 9899:2011 ] Subclause 7.21.3, "Files"
Excerpt from SEI CERT C Coding Standard: Rules for Developing Safe, Reliable, and Secure Systems (2016 Edition) and SEI CERT C Coding Standard [https://cmu-sei.github.io/secure-coding-standards/sei-cert-c-coding-standard/recommendations/input-output-fio/fio24-c], Copyright (C) 1995-2026 Carnegie Mellon University. See 9.4 3rd-Party Licenses in the documentation for full details.

Possible Messages

Key

Text

Severity

Disabled

file_already_open

Do not open a file that is already open.

None

False

Options

close_functions

close_functions

Type: dict[bauhaus.analysis.config.QualifiedName, int]

Default:

{
   '_close': 0,
   'close': 0,
   'fclose': 0
}
Functions closing a stream, mapped to the argument position of the stream.
 

open_functions

open_functions

Type: dict[bauhaus.analysis.config.QualifiedName, int]

Default:

{
   '_open': 0,
   '_wfopen': 0,
   '_wfopen_s': 1,
   '_wopen': 0,
   'fopen': 0,
   'fopen_s': 1,
   'open': 0,
   'openat': 1
}
Open function names mapped to the argument position of the filename string.