Fixed #4455 (redundantCopy when precleaning with memset)

This commit is contained in:
Daniel Marjamäki 2013-04-30 06:43:16 +02:00
parent 1f97e0e5df
commit 47ef8cf455
2 changed files with 14 additions and 4 deletions

View File

@ -612,6 +612,7 @@ void CheckOther::checkRedundantAssignment()
std::map<unsigned int, const Token*> varAssignments;
std::map<unsigned int, const Token*> memAssignments;
std::set<unsigned int> initialized;
const Token* writtenArgumentsEnd = 0;
for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
@ -672,10 +673,12 @@ void CheckOther::checkRedundantAssignment()
const Token* param1 = tok->tokAt(2);
writtenArgumentsEnd = param1->next();
if (param1->varId() && param1->strAt(1) == "," && !Token::Match(tok, "strcat|strncat|wcscat|wcsncat")) {
std::map<unsigned int, const Token*>::iterator it = memAssignments.find(param1->varId());
if (it == memAssignments.end())
if (tok->str() == "memset" && initialized.find(param1->varId()) == initialized.end() && param1->variable() && param1->variable()->isLocal() && param1->variable()->isArray())
initialized.insert(param1->varId());
else if (memAssignments.find(param1->varId()) == memAssignments.end())
memAssignments[param1->varId()] = tok;
else {
const std::map<unsigned int, const Token*>::iterator it = memAssignments.find(param1->varId());
if (scope->type == Scope::eSwitch && Token::findmatch(it->second, "default|case", tok) && warning)
redundantCopyInSwitchError(it->second, tok, param1->str());
else if (performance)

View File

@ -5643,8 +5643,7 @@ private:
" memcpy(a, b, 5);\n"
" memmove(a, b, 5);\n"
"}");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (performance) Buffer 'a' is being written before its old content has been used.\n"
"[test.cpp:3] -> [test.cpp:5]: (performance) Buffer 'a' is being written before its old content has been used.\n"
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:5]: (performance) Buffer 'a' is being written before its old content has been used.\n"
"[test.cpp:3]: (warning, inconclusive) Array 'a' is filled incompletely. Did you forget to multiply the size given to 'memset()' with 'sizeof(*a)'?\n"
"[test.cpp:4]: (warning, inconclusive) Array 'a' is filled incompletely. Did you forget to multiply the size given to 'memcpy()' with 'sizeof(*a)'?\n"
"[test.cpp:5]: (warning, inconclusive) Array 'a' is filled incompletely. Did you forget to multiply the size given to 'memmove()' with 'sizeof(*a)'?\n", errout.str());
@ -5949,6 +5948,14 @@ private:
" memset(a, 0, size);\n"
"}");
ASSERT_EQUALS("", errout.str());
// #4455 - initialization of local buffer
check("void f(void) {"
" char buf[10];\n"
" memset(buf, 0, 10);\n"
" strcpy(buf, string);\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void varFuncNullUB() { // #4482