Check for memset on objects with virtual functions (ticket #607)

This commit is contained in:
Greg Hewgill 2011-03-09 22:10:39 +13:00
parent 70b4076111
commit 3883afcbf4
2 changed files with 31 additions and 3 deletions

View File

@ -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);
}
//---------------------------------------------------------------------------

View File

@ -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()