Fixed #1287 (Use of memset on struct - std template types not handled)

This commit is contained in:
Daniel Marjamäki 2010-02-04 19:40:35 +01:00
parent 0ba665d77f
commit 9943262da0
2 changed files with 26 additions and 15 deletions

View File

@ -745,16 +745,8 @@ void CheckClass::noMemset()
if (!(type && type[0])) if (!(type && type[0]))
continue; continue;
// Warn if type is a class.. // Warn if type is a class or struct that contains any std::* variables
const std::string pattern1(std::string("class ") + type); const std::string pattern2(std::string("struct|class ") + type + " {");
if (Token::findmatch(_tokenizer->tokens(), pattern1.c_str()))
{
memsetClassError(tok, tok->str());
continue;
}
// Warn if type is a struct that contains any std::*
const std::string pattern2(std::string("struct ") + type + " {");
for (const Token *tstruct = Token::findmatch(_tokenizer->tokens(), pattern2.c_str()); tstruct; tstruct = tstruct->next()) for (const Token *tstruct = Token::findmatch(_tokenizer->tokens(), pattern2.c_str()); tstruct; tstruct = tstruct->next())
{ {
if (tstruct->str() == "}") if (tstruct->str() == "}")
@ -765,6 +757,12 @@ void CheckClass::noMemset()
memsetStructError(tok, tok->str(), tstruct->strAt(2)); memsetStructError(tok, tok->str(), tstruct->strAt(2));
break; break;
} }
if (Token::Match(tstruct, "std :: %type% < %type% > %var% ;"))
{
memsetStructError(tok, tok->str(), tstruct->strAt(2));
break;
}
} }
} }
} }

View File

@ -1436,18 +1436,18 @@ private:
"};\n" "};\n"
"void f()\n" "void f()\n"
"{\n" "{\n"
" A fail;\n" " A a;\n"
" memset(&fail, 0, sizeof(A));\n" " memset(&a, 0, sizeof(A));\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:7]: (error) Using 'memset' on class\n", errout.str()); ASSERT_EQUALS("", errout.str());
checkNoMemset("struct A\n" checkNoMemset("struct A\n"
"{\n" "{\n"
"};\n" "};\n"
"void f()\n" "void f()\n"
"{\n" "{\n"
" struct A fail;\n" " struct A a;\n"
" memset(&fail, 0, sizeof(A));\n" " memset(&a, 0, sizeof(A));\n"
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
@ -1481,6 +1481,19 @@ private:
ASSERT_EQUALS("[test.cpp:10]: (error) Using 'memset' on struct that contains a 'std::string'\n", errout.str()); ASSERT_EQUALS("[test.cpp:10]: (error) Using 'memset' on struct that contains a 'std::string'\n", errout.str());
} }
void memsetVector()
{
checkNoMemset("struct A\n"
"{ std::vector<int> ints; }\n"
"\n"
"void f()\n"
"{\n"
" A a;\n"
" memset(a, 0, sizeof(A));\n"
"}");
ASSERT_EQUALS("[test.cpp:7]: (error) Using 'memset' on struct that contains a 'std::vector'\n", errout.str());
}
void checkThisSubtraction(const char code[]) void checkThisSubtraction(const char code[])
{ {