Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

The address of the FILE object used to control a stream may be significant; a copy of a FILE object need not serve in place of the original. Do not use a copy of a FILE object in any input/output operations.

...

Code Block
bgColor#FFCCCC
#include <stdio.h>

int main(void) {
    FILE my_stdout = *(stdout);
    fputs("Hello, World!\n", &my_stdout);

    return 0;
}

For example, this non-compliant example fails with an "access violation" when compiled under Microsoft Visual Studio 2005 and run on an IA-32 platformunder Windows.

Compliant Solution

In this compliant solution, a copy of the pointer to the FILE object is used in the call to fputs().

Code Block
bgColor#ccccff
#include <stdio.h>

int main(void) {
    FILE *my_stdout = stdout;
    fputs("Hello, World!\n", my_stdout);
    return 0;
}

Risk Assessment

...