Fixed #5259 (Improve check: Uninitialized variable not reported when used in array initialization)

This commit is contained in:
Daniel Marjamäki 2019-08-24 08:01:55 +02:00
parent cb119f5910
commit 35fb55d76c
2 changed files with 16 additions and 0 deletions

View File

@ -973,6 +973,16 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, Alloc al
}
}
{
const Token *parent = vartok->astParent();
while (parent && parent->isCast())
parent = parent->astParent();
while (parent && parent->str() == ",")
parent = parent->astParent();
if (Token::simpleMatch(parent, "{"))
return true;
}
if (Token::Match(vartok->previous(), "++|--|%cop%")) {
if (mTokenizer->isCPP() && alloc == ARRAY && Token::Match(vartok->tokAt(-4), "& %var% =|( *"))
return false;

View File

@ -3978,6 +3978,12 @@ private:
" if (2 < sizeof(*x)) {}\n"
"}");
ASSERT_EQUALS("", errout.str());
valueFlowUninit("void foo() {\n" // #5259 - False negative
" int a;\n"
" int x[] = {a,2};\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());
}
void uninitvar_ipa() {