Fixed #4460: Ensure that memset/memcpy is called on a pointer to a class instance.

This commit is contained in:
PKEuS 2013-01-05 12:27:55 -08:00
parent 4c73c29cdd
commit f5ebbff0a3
2 changed files with 12 additions and 2 deletions

View File

@ -847,8 +847,8 @@ void CheckClass::noMemset()
else if (Token::Match(arg1, "&| %var% ,")) { else if (Token::Match(arg1, "&| %var% ,")) {
unsigned int varid = arg1->str() == "&" ? arg1->next()->varId() : arg1->varId(); unsigned int varid = arg1->str() == "&" ? arg1->next()->varId() : arg1->varId();
const Variable *var = symbolDatabase->getVariableFromVarId(varid); const Variable *var = symbolDatabase->getVariableFromVarId(varid);
if (var && (var->typeStartToken() == var->typeEndToken() || if (var && (var->typeStartToken() == var->typeEndToken() || Token::Match(var->typeStartToken(), "%type% :: %type%"))
Token::Match(var->typeStartToken(), "%type% :: %type%"))) && (arg1->str() == "&" || var->isPointer() || var->isArray()))
typeTok = var->typeEndToken(); typeTok = var->typeEndToken();
} }

View File

@ -2282,6 +2282,16 @@ private:
" memset(&a, 0, sizeof(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()); ASSERT_EQUALS("[test.cpp:7]: (error) Using 'memset' on struct that contains a 'std::vector'.\n", errout.str());
checkNoMemset("struct A {\n"
" std::vector<int *> buf;\n"
" operator int*() {return &buf[0];}\n"
"};\n"
"void f() {\n"
" A a;\n"
" memset(a, 0, 100);\n"
"}");
ASSERT_EQUALS("", errout.str()); // #4460
} }