Skip to content

Commit

Permalink
Restore cout format flags after modification
Browse files Browse the repository at this point in the history
As reported by Coverity Scan (issue 89189), pcp::pmda::display_version
was leaving the std::cout format flags altered.
  • Loading branch information
pcolby committed Apr 8, 2015
1 parent bcfa65c commit af388d9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions include/pcp-cpp/pmda.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,8 @@ class pmda {
*/
virtual void display_version() const
{
const std::ostream::fmtflags cout_format_flags(std::cout.flags());

std::cout << std::endl << get_pmda_name() << " PMDA";
const std::string pmda_version = get_pmda_version();
if (!pmda_version.empty()) {
Expand All @@ -765,6 +767,8 @@ class pmda {
<< get_pcp_runtime_version<char *>() << " ("
<< std::hex << get_pcp_runtime_version<uint_fast32_t>()
<< ')' << std::endl << std::endl;

std::cout.flags(cout_format_flags); // Restore cout to its entry state.
}

/**
Expand Down
11 changes: 11 additions & 0 deletions test/unit/src/test_pmda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,14 @@ TEST(pmda, store_value_throws_by_default) {
const pmValueBlock * const value = NULL;
EXPECT_THROW(pmda.store_value(metric_id, value), pcp::exception);
}

TEST(pmda, display_functions_do_not_leave_cout_flags_modified) {
const std::ostream::fmtflags flags(std::cout.flags());
const stub_pmda pmda;

pmda.display_help(pmda.get_pmda_name());
EXPECT_EQ(flags, std::cout.flags());

pmda.display_version();
EXPECT_EQ(flags, std::cout.flags());
}

0 comments on commit af388d9

Please sign in to comment.