We are catching a Coverity finding CID 156014: Not restoring ostream format (STREAM_FORMAT_STATE) (text below and image at the end).
938 const std::streamsize oldp = cout.precision(6);
5. format_changed: setf changes the format state of std::cout for category floatfield.
939 const std::ios::fmtflags oldf = cout.setf(std::ios::fixed, std::ios::floatfield);
940 cout << " Maurer Randomness Test returned value " << mv << endl;
6. format_changed: precision changes the format state of std::cout for category precision.
941 cout.precision(oldp);
7. format_restored: setf changes the format state of std::cout for category floatfield.
942 cout.setf(oldf, std::ios::floatfield);
It appears line 941, cout.precision(oldp)
is an issue. Coverity seems to classify it as a change rather than a restore.
According to set back default precision C++ on SO, I believe we are doing what is recommended. (But I could be wrong, or the accepted answer may not be a best practice).
How can we restore precision and squash the Coverity finding?
I recently tired the following, but it continues to produce the finding format_changed: setf changes the format state of std::cout for category floatfield
.
// Coverity finding
class StreamState
{
public:
StreamState(std::ostream& out)
: m_out(out), m_fmt(out.flags()), m_prec(out.precision())
{
}
~StreamState()
{
m_out.precision(m_prec);
m_out.flags(m_fmt);
}
private:
std::ostream& m_out;
std::ios_base::fmtflags m_fmt;
std::streamsize m_prec;
};
...
StreamState ss(cout);
cout.setf(std::ios::fixed, std::ios::floatfield);
cout.precision(6);
cout << " Maurer Randomness Test returned value " << mv << endl;