You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 27 Next »

The C99 fopen() function can open streams to which both input and output may be performed. This functionality is known as "update" mode and is signified by using '{+}' as the second or third character in the mode string to fopen().

However, section 7.19.5.3 of C99 places the following restrictions on update streams:

When a file is opened with update mode ('{+}' as the second or third character in the above list of mode argument values), both input and output may be performed on the associated stream. However, output shall not be directly followed by input without an intervening call to the fflush function or to a file positioning function (fseek, fsetpos, or rewind), and input shall not be directly followed by output without an intervening call to a file positioning function, unless the input operation encounters end-of-file. Opening (or creating) a text file with update mode may instead open (or create) a binary stream in some implementations.

Receiving input from a stream directly following an output to that stream without an intervening call to fflush(), fseek(), fsetpos(), or rewind(), or outputting to a stream after receiving input from it without a call to fseek(), fsetpos(), rewind() if the file is not at end-of-file results in undefined behavior. Consequently, a call to one of these functions is necessary between input and output to the same stream.

Non-Compliant Code Example

The following non-compliant code appends data to a file and then reads from the same file.

char data[BUFSIZ];
char append_data[BUFSIZ];
char *file_name;
FILE *file;

/* initialize file_name */

file = fopen(file_name, "a+");
if (file == NULL) {
  /* handle error */
}

/* initialize append_data */

if (fwrite(append_data, BUFSIZ, 1, file) != BUFSIZ) {
  /* handle error */
}
if (fread(data, BUFSIZ, 1, file) != 0) {
  /* handle there not being data */
}

fclose(file);

However, because the stream is not flushed in between the call to fread() and fwrite(), the behavior is undefined.

Compliant Solution

In this compliant solution, fflush() is called in between the output and input, eliminating the undefined behavior.

char data[BUFSIZ];
char append_data[BUFSIZ];
char *file_name;
FILE *file;

/* initialize file_name */

file = fopen(file_name, "a+");
if (file == NULL) {
  /* handle error */
}

/* initialize append_data */

if (fwrite(append_data, BUFSIZ, 1, file) != BUFSIZ) {
  /* Handle error */
}
fflush(file);
if (fread(data, BUFSIZ, 1, file) != 0) {
  /* handle there not being data */
}

fclose(file);

Risk Assessment

Alternately inputting and outputting from a stream without an intervening flush or positioning call results in undefined behavior.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

FIO39-C

low

likely

medium

P6

L2

Automated Detection

Fortify SCA Version 5.0 with CERT C Rule Pack can detect violations of this rule.

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

References

[[ISO/IEC 9899:1999]] Section 7.19.5.3, "The fopen function"


FIO38-C. Do not use a copy of a FILE object for input and output      09. Input Output (FIO)       FIO40-C. Reset strings on fgets() failure

  • No labels