Uninitialized variables: Fixed false positive when address of variable is taken inside = { .. }. Ticket: #3369

This commit is contained in:
Daniel Marjamäki 2011-12-17 07:56:46 +01:00
parent 0572321572
commit dfedb920f8
2 changed files with 22 additions and 3 deletions

View File

@ -1126,9 +1126,19 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const unsigned int
}
}
// skip = { .. }
if (Token::simpleMatch(tok, "= {"))
tok = tok->next()->link();
// = { .. }
if (Token::simpleMatch(tok, "= {")) {
// end token
const Token *end = tok->next()->link();
// If address of variable is taken in the block then bail out
if (Token::findmatch(tok->tokAt(2), "& %varid%", end, varid))
return true;
// Skip block
tok = end;
continue;
}
// TODO: handle loops, try, etc
if (tok->str() == "for" || Token::simpleMatch(tok, ") {") || Token::Match(tok, "%var% {")) {

View File

@ -1732,6 +1732,15 @@ private:
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: x\n", errout.str());
// = { .. }
checkUninitVar2("int f() {\n"
" int a;\n"
" int *p[] = { &a };\n"
" *p[0] = 0;\n"
" return a;\n"
"}");
ASSERT_EQUALS("", errout.str());
// >> => initialization / usage
{
const char code[] = "void f() {\n"