AutosarC++18_10-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
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | automated |
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 [10]: FIO39-C: Do not alternately input and output from a stream without an intervening flush or positioning call
- SEI CERT C++ Coding Standard [10]: 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¶
This rule shares the following common options: confidence, exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
consider_std_fstream¶
consider_std_fstream : bool = True
std::fstream should be considered.
consider_stlib_file_stream¶
consider_stlib_file_stream : bool = False
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'}
operator>>
is always considered as a reading function.
report_non_local_streams¶
report_non_local_streams : bool = False
resetting_functions¶
resetting_functions
Possible functions that reset the stream or file for subsequent reading or writing. Any reassignment to a stream or file counts as resetting.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'}
writing_functions¶
writing_functions : set[bauhaus.analysis.config.QualifiedName] = {'fwrite', 'std::basic_ostream::put', 'std::basic_ostream::write'}
operator<< is always considered as a writing function.