Alternating input and output from a stream without flush or positioning call
Undefined behavior for input or output stream operations
Description
This defect occurs when you perform an output operation on a file stream following an input operation or vice versa, without an intermediate repositioning of the file stream. The defect checker detects issues in file operations using one of these file pointers:
A
FILE*
pointer (for files opened using functions such asfopen()
).An
std::basic_filebuf
object (for files opened using functions such asstd::fstream()
)
Risk
If you open a file stream for both reading and writing, you must perform an intermediate stream repositioning operation between read and write operations on the stream.
When you open a file stream using a function such as std::fstream()
, an std::basic_filebuf
object is created to track the position in the file stream where the next read or write operation will occur. The implementation of this object uses the C FILE*
abstraction. According to the C standard, a FILE*
pointer can be used to perform both input and output operations on a file stream. However, before you perform
an output operation following an input (or vice versa), you must first reposition the pointer in the file stream; otherwise, the behavior is undefined.
Fix
If you open a file stream for both reading and writing, you must perform an intermediate stream repositioning operation between read and write operations on the stream.
For
std::basic_filebuf
objects, you can reposition a file stream using methods such asstd::fstream<>::seekp()
orstd::fstream<>::seekg()
.For
FILE*
pointers, you can call a file positioning function such asfseek()
orfsetpos()
between output and input operations (or call thefflush()
function to flush the output buffer between an output and a subsequent input operation).
Examples
Result Information
Group:Programming |
Language: C | C++ |
Default: On for handwritten code, off for generated code |
Command-Line Syntax:
IO_INTERLEAVING
|
Impact: Low |
Version History
Introduced in R2017bSee Also
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)