Improved fix for #4455, no false negatives if variable is used before first memset

This commit is contained in:
Daniel Marjamäki 2013-04-30 16:56:44 +02:00
parent cf75e2bb28
commit b2798e929d
2 changed files with 18 additions and 0 deletions

View File

@ -629,6 +629,17 @@ void CheckOther::checkRedundantAssignment()
varAssignments.clear();
memAssignments.clear();
} else if (tok->type() == Token::eVariable) {
// Set initialization flag
if (!Token::Match(tok, "%var% ["))
initialized.insert(tok->varId());
else {
const Token *tok2 = tok->next();
while (tok2 && tok2->str() == "[")
tok2 = tok2->link()->next();
if (tok2 && tok2->str() != ";")
initialized.insert(tok->varId());
}
std::map<unsigned int, const Token*>::iterator it = varAssignments.find(tok->varId());
if (tok->next()->isAssignmentOp() && Token::Match(tok->previous(), "[;{}]")) { // Assignment
if (it != varAssignments.end()) {

View File

@ -5956,6 +5956,13 @@ private:
" strcpy(buf, string);\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f(void) {\n"
" char buf[10] = {0};\n"
" memset(buf, 0, 10);\n"
" strcpy(buf, string);\n"
"}");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (performance) Buffer 'buf' is being written before its old content has been used.\n", errout.str());
}
void varFuncNullUB() { // #4482