| Wiki Markup |
|---|
Simultaneously opening a file multiple times has implementation-defined behavior. On some platforms, this is not allowed. On others, it might result in race conditions. h2. |
...
Non-Compliant Coding Example |
...
The following non-compliant code example logs the program's state at runtime. |
...
{code | ||
:bgColor | =#ffcccc | "
void do_stuff(void) {
FILE *logfile = fopen("log", "a");
/* Check for errors, write logs pertaining to do_stuff(), etc. */
}
int main(void) {
FILE *logfile = fopen("log", "a");
/* Check for errors, write logs pertaining to main(), etc. */
do_stuff();
/* ... */
}
{null}
However, the file {{log}} is opened twice simultaneously. The result is implementation-defined and potentially dangerous. |
...
h2. Compliant Solution |
...
In this compliant solution, a reference to the file pointer is passed around so that the file does not have to be opened twice separately. |
...
{code | ||
:bgColor | =#ccccff | } void do_stuff(FILE **file) { FILE *logfile = *file; /* Check for errors, write logs pertaining to do_stuff, etc. */ } int main(void) { FILE *logfile = fopen("log", "a"); /* Check for errors, write logs pertaining to main, etc. */ do_stuff(&logfile); /* ... */ } {code} h2. Risk Assessment |
...
Simultaneously opening a file multiple times could result in abnormal program termination or a data integrity violation. |
...
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
|---|---|---|---|---|---|
FIO31-A | 2 (medium) | 2 (probable) | 2 (medium) | P8 | L2 |
References
...
|| Rule || Severity || Likelihood || Remediation Cost || Priority || Level || | FIO31-A | *2* (medium) | *2* (probable) | *2* (medium) | {color:#cc9900}{*}P8{*}{color} | {color:#cc9900}{*}L2{*}{color} | h2. References \[[ISO/IEC 9899-1999:TC2|AA. C References#ISO/IEC 9899-1999TC2]\] Section 7.19.3, "Files" |