封面发现:无法恢复ostream格式(STREAM_FORMAT_STATE) [英] Coverity finding: Not restoring ostream format (STREAM_FORMAT_STATE)
问题描述
我们正在寻找Coverity查找 CID 156014:无法恢复ostream格式(STREAM_FORMAT_STATE) (下面的文字和最后的图片)。
938 const std :: streamsize oldp = cout.precision(6);
5. format_changed:setf更改类型floatfield的std :: cout的格式状态。
939 const std :: ios :: fmtflags oldf = cout.setf(std :: ios :: fixed,std :: ios :: floatfield);
940 cout<< Maurer Randomness Test returns value< mv < endl;
6. format_changed:precision更改std :: cout的格式状态以获取类别精度。
941 cout.precision(oldp);
7. format_restored:setf更改类型floatfield的std :: cout的格式状态。
942 cout.setf(oldf,std :: ios :: floatfield);
出现第941行, cout.precision(oldp)
是一个问题。 Coverity似乎将其分为 更改 ,而不是恢复 。
根据
我最近累了下面的内容,但它继续产生这个结果 format_changed:setf更改std的格式状态:
//封面找寻$ b $ b class StreamState
{
public:
StreamState(std :: ostream& out)
:m_out(out),m_fmt(out.flags()),m_prec out.precision())
{
}
〜StreamState()
{
m_out.precision
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 returns value< mv < endl;
我们提交的一周),我们不能 使用成员函数在程序中设置 floatfield
即 ostream.iosflags(...); ostream.precision(...)
)。我们不得不使用 floatfield
(即 ostream<< setiosflags(...) < setprecision(...)
):
/ Coverity查找
类StreamState
{
public:
StreamState(std :: ostream& out)
:m_out(out),m_fmt(out.flags ),m_prec(out.precision())
{
}
〜StreamState()
{
m_out.precision
m_out.flags(m_fmt);
}
private:
std :: ostream& m_out;
std :: ios_base :: fmtflags m_fmt;
std :: streamsize m_prec;
};
和:
StreamState ss(cout);
cout< std :: setiosflags(std :: ios :: fixed)<< std :: setprecision(6);
cout< Maurer Randomness Test returns value< mv < endl;
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;
After about 6 attempts (I think we used all of our submissions for the week), it appears we cannot use the member functions to set the floatfield
in the program (i.e., ostream.iosflags(...); ostream.precision(...)
). We had to use the manipulators for the floatfield
(i.e., ostream << setiosflags(...) << setprecision(...)
):
// 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;
};
And:
StreamState ss(cout);
cout << std::setiosflags(std::ios::fixed) << std::setprecision(6);
cout << " Maurer Randomness Test returned value " << mv << endl;
这篇关于封面发现:无法恢复ostream格式(STREAM_FORMAT_STATE)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!