Coverity finding: Not restoring ostream format (STREAM_FORMAT_STATE)

試著忘記壹切 提交于 2019-12-24 16:42:52

问题


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;

回答1:


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;


来源:https://stackoverflow.com/questions/34503914/coverity-finding-not-restoring-ostream-format-stream-format-state

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!