Uninitialized variables: better handling of struct variables

This commit is contained in:
Daniel Marjamäki 2010-05-30 07:55:11 +02:00
parent 46b5e4e79a
commit 406cbda563
2 changed files with 46 additions and 19 deletions

View File

@ -2832,6 +2832,16 @@ private:
return vartok;
}
if (Token::Match(tok.previous(), "[;{}] struct %type% *| %var% ;"))
{
const Token * vartok = tok.tokAt(2);
const bool p(vartok->str() == "*");
if (p)
vartok = vartok->next();
declare(checks, vartok, tok, p, false);
return vartok;
}
// Variable declaration for array..
if (Token::Match(tok.previous(), "[;{}] %type% %var% [ %num% ] ;"))
{
@ -2875,7 +2885,16 @@ private:
return &tok;
}
if (Token::Match(tok.previous(), "[;{}] %var% =|["))
if (Token::Match(tok.previous(), "[;{}] %var% =|[|."))
{
if (tok.next()->str() == ".")
{
if (use_dead_pointer(checks, &tok))
{
return &tok;
}
}
else
{
// check variable usages in rhs/index
for (const Token *tok2 = tok.tokAt(2); tok2; tok2 = tok2->next())
@ -2901,6 +2920,7 @@ private:
}
}
}
}
// pointer aliasing?
if (Token::Match(tok.tokAt(2), "%var% ;"))

View File

@ -1367,6 +1367,13 @@ private:
"}\n");
ASSERT_EQUALS("", errout.str());
checkUninitVar("void a()\n"
"{\n"
" struct S *s;\n"
" s->x = 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: s\n", errout.str());
// #1533
checkUninitVar("char a()\n"
"{\n"