Uninitialized variables: tweaked checking of arrays / allocated buffers

This commit is contained in:
Daniel Marjamäki 2015-09-14 09:03:21 +02:00
parent 6647976d80
commit af233efcce
2 changed files with 8 additions and 12 deletions

View File

@ -838,11 +838,7 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, Alloc al
}
bool unknown = false;
if (pointer && CheckNullPointer::isPointerDeRef(vartok, unknown)) {
// pointer is allocated - dereferencing it is ok.
if (alloc != NO_ALLOC)
return false;
if (pointer && alloc == NO_ALLOC && CheckNullPointer::isPointerDeRef(vartok, unknown)) {
// function parameter?
bool functionParameter = false;
if (Token::Match(vartok->tokAt(-2), "%name% (") || vartok->previous()->str() == ",")
@ -851,12 +847,14 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, Alloc al
// if this is not a function parameter report this dereference as variable usage
if (!functionParameter)
return true;
} else if (alloc == ARRAY && Token::Match(vartok, "%var% [")) {
} else if (alloc != NO_ALLOC && Token::Match(vartok, "%var% [")) {
const Token *parent = vartok->next()->astParent();
while (Token::simpleMatch(parent, "["))
parent = parent->astParent();
if (Token::simpleMatch(parent, "&") && !parent->astOperand2())
return false;
if (parent && Token::Match(parent->previous(), "if|while|switch ("))
return true;
if (Token::Match(parent, "[=,(]"))
return false;
return true;

View File

@ -1493,19 +1493,17 @@ private:
"}");
TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Memory is allocated but not initialized: p\n", "", errout.str());
checkUninitVar("void f()\n"
"{\n"
checkUninitVar("void f() {\n"
" char *p = malloc(64);\n"
" if (p[0]) { }\n"
"}");
TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Memory is allocated but not initialized: p\n", "", errout.str());
ASSERT_EQUALS("[test.cpp:3]: (error) Memory is allocated but not initialized: p\n", errout.str());
checkUninitVar("void f()\n"
"{\n"
checkUninitVar("void f() {\n"
" char *p = malloc(64);\n"
" return p[0];\n"
"}");
TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Memory is allocated but not initialized: p\n", "", errout.str());
ASSERT_EQUALS("[test.cpp:3]: (error) Memory is allocated but not initialized: p\n", errout.str());
checkUninitVar("void f()\n"
"{\n"