diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 648c562d1..c01baed55 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -743,7 +743,7 @@ void CheckClass::checkMemsetType(const Token *tok, const std::string &type) tstruct->str().find(":") != std::string::npos) { if (Token::Match(tstruct->next(), "std :: %type% %var% ;")) - memsetError(tok, tok->str(), tstruct->strAt(3), typeName); + memsetError(tok, tok->str(), "'std::" + tstruct->strAt(3) + "'", typeName); else if (Token::Match(tstruct->next(), "std :: %type% <")) { @@ -771,8 +771,10 @@ void CheckClass::checkMemsetType(const Token *tok, const std::string &type) // found error => report if (Token::Match(tstruct, "> %var% ;")) - memsetError(tok, tok->str(), typestr, typeName); + memsetError(tok, tok->str(), "'std::" + typestr + "'", typeName); } + else if (Token::simpleMatch(tstruct->next(), "virtual")) + memsetError(tok, tok->str(), "virtual method", typeName); } } } @@ -816,7 +818,7 @@ void CheckClass::noMemset() void CheckClass::memsetError(const Token *tok, const std::string &memfunc, const std::string &classname, const std::string &type) { - reportError(tok, Severity::error, "memsetClass", "Using '" + memfunc + "' on " + type + " that contains a 'std::" + classname + "'"); + reportError(tok, Severity::error, "memsetClass", "Using '" + memfunc + "' on " + type + " that contains a " + classname); } //--------------------------------------------------------------------------- diff --git a/test/testclass.cpp b/test/testclass.cpp index a724285df..ff0da1953 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -2961,6 +2961,32 @@ private: " memset(&pebbles, 0, sizeof(pebbles));\n" "}\n"); ASSERT_EQUALS("[test.cpp:9]: (error) Using 'memset' on class that contains a 'std::string'\n", errout.str()); + + checkNoMemset("class Fred\n" + "{\n" + " virtual ~Fred();\n" + "};\n" + "void f()\n" + "{\n" + " Fred fred;\n" + " memset(&fred, 0, sizeof(fred));\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:8]: (error) Using 'memset' on class that contains a virtual method\n", errout.str()); + + checkNoMemset("class Fred\n" + "{\n" + "};\n" + "class Wilma\n" + "{\n" + " virtual ~Wilma();\n" + "};\n" + "class Pebbles: public Fred, Wilma {};\n" + "void f()\n" + "{\n" + " Pebbles pebbles;\n" + " memset(&pebbles, 0, sizeof(pebbles));\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:12]: (error) Using 'memset' on class that contains a virtual method\n", errout.str()); } void memsetOnStruct()