Uninitialized variables: tweaked checking of arrays / allocated buffers
This commit is contained in:
parent
6647976d80
commit
af233efcce
|
@ -838,11 +838,7 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, Alloc al
|
||||||
}
|
}
|
||||||
|
|
||||||
bool unknown = false;
|
bool unknown = false;
|
||||||
if (pointer && CheckNullPointer::isPointerDeRef(vartok, unknown)) {
|
if (pointer && alloc == NO_ALLOC && CheckNullPointer::isPointerDeRef(vartok, unknown)) {
|
||||||
// pointer is allocated - dereferencing it is ok.
|
|
||||||
if (alloc != NO_ALLOC)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
// function parameter?
|
// function parameter?
|
||||||
bool functionParameter = false;
|
bool functionParameter = false;
|
||||||
if (Token::Match(vartok->tokAt(-2), "%name% (") || vartok->previous()->str() == ",")
|
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 this is not a function parameter report this dereference as variable usage
|
||||||
if (!functionParameter)
|
if (!functionParameter)
|
||||||
return true;
|
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();
|
const Token *parent = vartok->next()->astParent();
|
||||||
while (Token::simpleMatch(parent, "["))
|
while (Token::simpleMatch(parent, "["))
|
||||||
parent = parent->astParent();
|
parent = parent->astParent();
|
||||||
if (Token::simpleMatch(parent, "&") && !parent->astOperand2())
|
if (Token::simpleMatch(parent, "&") && !parent->astOperand2())
|
||||||
return false;
|
return false;
|
||||||
|
if (parent && Token::Match(parent->previous(), "if|while|switch ("))
|
||||||
|
return true;
|
||||||
if (Token::Match(parent, "[=,(]"))
|
if (Token::Match(parent, "[=,(]"))
|
||||||
return false;
|
return false;
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -1493,19 +1493,17 @@ private:
|
||||||
"}");
|
"}");
|
||||||
TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Memory is allocated but not initialized: p\n", "", errout.str());
|
TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Memory is allocated but not initialized: p\n", "", errout.str());
|
||||||
|
|
||||||
checkUninitVar("void f()\n"
|
checkUninitVar("void f() {\n"
|
||||||
"{\n"
|
|
||||||
" char *p = malloc(64);\n"
|
" char *p = malloc(64);\n"
|
||||||
" if (p[0]) { }\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"
|
checkUninitVar("void f() {\n"
|
||||||
"{\n"
|
|
||||||
" char *p = malloc(64);\n"
|
" char *p = malloc(64);\n"
|
||||||
" return p[0];\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"
|
checkUninitVar("void f()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
|
|
Loading…
Reference in New Issue