According to the C Standard, subclause 7.21.3, paragraph 6 [ISO/IEC 9899:2011],
...
This noncompliant code example can fail because a by-value copy of stdout is being used in the call to fputs():
| Code Block | ||||
|---|---|---|---|---|
| ||||
#include <stdio.h>
int main(void) {
FILE my_stdout = *(stdout);
if (fputs("Hello, World!\n", &my_stdout) == EOF) {
/* Handle error */
}
return 0;
}
|
For example, this This noncompliant example raises an "access violation" exception at runtime when compiled under Microsoft Visual Studio 2013 and run under Windows.
...
In this compliant solution, a copy of the stdout pointer to the FILE object is used in the call to fputs():
...
| CERT C++ Secure Coding Standard | FIO38-CPP. Do not use a copy of a FILE object for input and output |
| ISO/IEC TS 17961:2013 | Copying a FILE object [filecpy] |
Bibliography
| [ISO/IEC 9899:2011] | Subclause 7.21.3, "Files" |
...