AutosarC++18_03-A27.0.3

Alternate input and output operations on a file stream shall not be used without an intervening flush or positioning call

Required inputs: IR, StaticSemanticAnalysis

Rationale

There are following restrictions on reading and writing operations called for an object of class basic_filebuf<charT, traits>:

  • 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).
  • 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.

It is recommended to use a file stream either for output (std::ofstream) or input(std:: ifstream) and not for both in the same context. This avoids the mentioned problem altogether.

Example
// $Id: A27-0-3.cpp 311495 2018-03-13 13:02:54Z michal.szczepankiewicz $

#include <fstream>
#include <string>

int main(void) {
  std::fstream f("testfile");

  f << "Output";
  std::string str1;
  f >> str1; // non-compliant

  f << "More output";
  std::string str2;
  f.seekg(0, std::ios::beg);
  f >> str2; // compliant

  return 0;
}
See Also
  • SEI CERT C++ Coding Standard [9]: FIO39-C: Do not alternately input and output from a stream without an intervening flush or positioning call
  • SEI CERT C++ Coding Standard [9]: FIO50-CPP: Do not alternately input and output from a file stream without an intervening positioning call

Possible Messages

Key

Text

Severity

Disabled

alternating_input_output

Alternating input and output without intervening flush or positioning.

None

False

possibly_alternating_input_output

Possibly alternating input and output without intervening flush or positioning. References a stream from outside the routine.

None

False

Options

consider_std_fstream

consider_std_fstream : bool = True

Whether streams based on std::fstream should be considered.
 

consider_stlib_file_stream

consider_stlib_file_stream : bool = False

Whether streams based on stdio.h FILE streams should be considered.
 

reading_functions

reading_functions : set[bauhaus.analysis.config.QualifiedName] = {'fread', 'std::basic_istream::get', 'std::basic_istream::read', 'std::getline'}

Reading functions whose calls should not be followed by writing function calls without an intervening call to a resetting function call. operator>> is always considered as a reading function.
 

report_non_local_streams

report_non_local_streams : bool = False

If set to true, (possible) violations are reported for streams that are not local to routines and could thus be modified elsewhere.
 

resetting_functions

resetting_functions

Type: set[bauhaus.analysis.config.QualifiedName]

Default: {'fflush', 'fseek', 'fsetpos', 'rewind', 'std::basic_istream::seekg', 'std::basic_istream::tellg', 'std::basic_ostream::flush', 'std::basic_ostream::seekp', 'std::basic_ostream::tellp', 'std::basic_streambuf::pubseekoff', 'std::basic_streambuf::pubseekpos', 'std::flush'}

Possible functions that reset the stream or file for subsequent reading or writing. Any reassignment to a stream or file counts as resetting.
 

writing_functions

writing_functions : set[bauhaus.analysis.config.QualifiedName] = {'fwrite', 'std::basic_ostream::put', 'std::basic_ostream::write'}

Writing functions whose calls should not be followed by reading function calls without an intervening call to a resetting function call. operator<< is always considered as a writing function.