Uninitialized variables: better checking of arrays

This commit is contained in:
Daniel Marjamäki 2015-09-13 16:32:16 +02:00
parent 1fd9ba0cc4
commit ad007ca2d7
2 changed files with 17 additions and 2 deletions

View File

@ -845,6 +845,15 @@ 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% [")) {
const Token *parent = vartok->next()->astParent();
while (Token::simpleMatch(parent, "["))
parent = parent->astParent();
if (Token::simpleMatch(parent, "&") && !parent->astOperand2())
return false;
if (Token::Match(parent, "[=,(]"))
return false;
return true;
}
if (_tokenizer->isCPP() && Token::Match(vartok->next(), "<<|>>")) {

View File

@ -1407,13 +1407,19 @@ private:
" int a[1];\n"
" return a[0];\n"
"}");
TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", "", errout.str());
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());
checkUninitVar("int foo() {\n"
" int a[2][2];\n"
" return a[0][1];\n"
"}");
TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", "", errout.str());
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());
checkUninitVar("int foo() {\n"
" int a[10];\n"
" dostuff(a[0]);\n"
"}");
ASSERT_EQUALS("", errout.str());
// # 4740
checkUninitVar("void f() {\n"