CertC-FIO38¶
Do not copy a FILE object
Required inputs: IR
According to the C Standard, 7.23.3, paragraph 6 [ ISO/IEC 9899:2024 ],
The address of the
FILEobject used to control a stream may be significant; a copy of aFILEobject is not required to serve in place of the original.
Consequently, do not copy a FILE object.
Noncompliant Code Example
This noncompliant code example can fail because a by-value copy of stdout is being used in the call to fputs() :
#include <stdio.h>
int main(void) {
FILE my_stdout = *stdout;
if (fputs("Hello, World!\n", &my_stdout) == EOF) {
/* Handle error */
}
return 0;
}
|
When compiled under Microsoft Visual Studio 2013 and run on Windows, this noncompliant example results in an "access violation" at runtime.
Compliant Solution
In this compliant solution, a copy of the stdout pointer to the FILE object is used in the call to fputs() :
#include <stdio.h>
int main(void) {
FILE *my_stdout = stdout;
if (fputs("Hello, World!\n", my_stdout) == EOF) {
/* Handle error */
}
return 0;
}
|
Risk Assessment
Using a copy of a FILE object in place of the original may result in a crash, which can be used in a denial-of-service attack .
| Rule | Severity | Likelihood | Detectable | Repairable | Priority | Level |
|---|---|---|---|---|---|---|
| FIO38-C | Low | Probable | Yes | No | P4 | L3 |
Related Guidelines
| Taxonomy | Taxonomy item | Relationship |
| ISO/IEC TS 17961:2013 | Copying a FILE object [filecpy] |
Prior to 2018-01-12: CERT: Unspecified Relationship |
Bibliography
| [ ISO/IEC 9899:2024 ] | 7.23.3, "Files" |
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
file_pointer_dereference |
A pointer to a FILE object shall not be dereferenced |
None |
False |
Options
This rule shares the following common options: confidence, exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
This rule has no individual options.