Simultaneously opening a file multiple times has implementation-defined behavior. While some platforms may forbid a file simultaneously being opened multiple times, those that allow it may facilitate dangerous race conditions.
...
Noncompliant Code Example
The following non-compliant noncompliant code example logs the program's state at runtime.
| Code Block | ||
|---|---|---|
| ||
void do_stuff(void) {
FILE *logfile = fopen("log", "a");
if (logfile == NULL) {
/* handle error */
}
/* write logs pertaining to do_stuff() */
/* ... */
}
int main(void) {
FILE *logfile = fopen("log", "a");
if (logfile == NULL) {
/* handle error */
}
/* write logs pertaining to main() */
do_stuff();
/* ... */
}
|
...
| Code Block | ||
|---|---|---|
| ||
void do_stuff(FILE *logfile) {
/* write logs pertaining to do_stuff() */
/* ... */
}
int main(void) {
FILE *logfile = fopen("log", "a");
if (logfile == NULL) {
/* handle error */
}
/* write logs pertaining to main() */
do_stuff(logfile);
/* ... */
}
|
...