CertC-FIO46

Do not access a closed file

Required inputs: IR, StaticSemanticAnalysis

Using the value of a pointer to a FILE object after the associated file is closed is undefined behavior . (See undefined behavior 153 .) Programs that close the standard streams (especially stdout but also stderr and stdin ) must be careful not to use these streams in subsequent function calls, particularly those that implicitly operate on them (such as printf() , perror() , and getc() ).

This rule can be generalized to other file representations.

Noncompliant Code Example

In this noncompliant code example, the stdout stream is used after it is closed:

#include <stdio.h>

int close_stdout(void) {
  if (fclose(stdout) == EOF) {
    return -1;
  }

  printf("stdout successfully closed.\n");
  return 0;
}
Compliant Solution

In this compliant solution, stdout is not used again after it is closed. This must remain true for the remainder of the program, or stdout must be assigned the address of an open file object.

#include <stdio.h>

int close_stdout(void) {
  if (fclose(stdout) == EOF) {
    return -1;
  }

  fputs("stdout successfully closed.", stderr);
  return 0;
}
Risk Assessment

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

Rule Severity Likelihood Detectable Repairable Priority Level
FIO46-C Medium Unlikely No No P2 L3
Bibliography
[ IEEE Std 1003.1:2013 ] XSH, System Interfaces, open
[ ISO/IEC 9899:2024 ] Subclause 7.23.3, "Files"
Subclause 7.23.5.1, "The fclose Function"
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/rules/input-output-fio/fio46-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

possible_use_after_free

Possibly access to a closed file

None

False

use_after_free

Do not access a closed file

None

False

Options

functions_with_ignored_deallocators

functions_with_ignored_deallocators : set[str] = set()

Set of functions (given by their qualified name) where all deallocators are ignored. For these functions, the check will never report a use-after-free. It will also assume that these functions never create freed pointers, neither by return value, out param, nor by modifying global state.
 

maximum_number_of_threads

maximum_number_of_threads : int = 0

This option determines how many threads are used during computation of this rule at most. With 0, the maximum number of threads is automatically determined with a heuristic that considers the number of available CPU cores as well as the available memory.
 

report_freed_this_at_call

report_freed_this_at_call : bool = False

This option controls findings when a freed pointer is used in C++ to call a non-static member function. When set to true, the use at the call is directly reported. When false, the analysis waits for an actual dereference (of the this-pointer then) inside the callee, and only reports those.
 

report_read_pointer_args_in_calls_to_undefined

report_read_pointer_args_in_calls_to_undefined : bool = True

Report when freed pointers are passed to undefined (external) functions.
 

resources

resources : set[str] = {'FileHandle'}

Set of resources to be checked (selection of rules in the Resources group).
 

witness_paths

witness_paths : bool = True

Whether witness paths should be determined and included in the issue.