Versions Compared

Key

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

The C99 fopen() function has the capability to open files to which both input and output are capable. 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 restriction 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.

Therefore either receiving Receiving input from a stream directly following an output to that stream (or vice versa) 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 in between input and output to the same stream in most cases.

Non-Compliant Code Example

In this The following non-compliant code example, a device is opened for updating, data are sent to it, and then the response is read backreads data from a presumably random area of a file, and then appends data to the end of it.

Code Block
bgColor#ffcccc
char data[BUF_SIZE];
char append_data[BUF_SIZE];
size_t append_size;
FILE *file = fopen(file_name, "rba+");
if (file == NULL) {
  /* handle error */
}

if (fread(data, BUF_SIZ, 1, file) != 0) {
  /* writeHandle there tonot filebeing streamdata */
}

/* Do something with data 
   Initialize append_data and append_size * read response from file stream *//

if(fwrite(append_data, append_size, 1, data) != append_size) {
  /* Handle error */
}

fclose(file);

However, since the output buffer stream is not flushed before receiving input back from the stream, so the data may not have actually been sent, resulting in unexpected behaviorin between the call to fread() and fwrite(), undefined behavior results.

Compliant Solution

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

Code Block
bgColor#ccccff#ffcccc

char data[BUF_SIZE];
char append_data[BUF_SIZE];
size_t append_size;

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

if (fread(data, BUF_SIZ, 1, file) != 0) {
  /* Handle there not being data */
}

/* write to file streamDo something with data 
   Initialize append_data and append_size */

fflush(file);
/* read response from file stream */

if(fwrite(append_data, append_size, 1, data) != append_size) {
  /* Handle error */
}

fclose(file);

This flush ensures that all data has been cleared from the buffer before continuing.

Risk Assessment

Failing to flush the output buffer may result in data not being sent over the streamTODO, causing unexpected program behavior and possibly a data integrity violation.

...

Wiki Markup
\[[ISO/IEC 9899:1999|AA. C References#ISO/IEC 9899-1999]\] Section 7.919.155.3, "The {{fopen}} function"

...