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; 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.. // Variable declaration for array..
if (Token::Match(tok.previous(), "[;{}] %type% %var% [ %num% ] ;")) if (Token::Match(tok.previous(), "[;{}] %type% %var% [ %num% ] ;"))
{ {
@ -2875,29 +2885,39 @@ private:
return &tok; return &tok;
} }
if (Token::Match(tok.previous(), "[;{}] %var% =|[")) if (Token::Match(tok.previous(), "[;{}] %var% =|[|."))
{ {
// check variable usages in rhs/index if (tok.next()->str() == ".")
for (const Token *tok2 = tok.tokAt(2); tok2; tok2 = tok2->next())
{ {
if (Token::Match(tok2, ";|)|=")) if (use_dead_pointer(checks, &tok))
break;
if (Token::Match(tok2, "%var% ("))
break;
if (tok2->varId() &&
!Token::Match(tok2->previous(), "&|::") &&
!Token::simpleMatch(tok2->next(), "="))
{ {
bool foundError; return &tok;
if (tok2->next()->str() == "[") }
foundError = use_array_or_pointer_data(checks, tok2); }
else else
foundError = use(checks, tok2); {
// check variable usages in rhs/index
// prevent duplicate error messages for (const Token *tok2 = tok.tokAt(2); tok2; tok2 = tok2->next())
if (foundError) {
if (Token::Match(tok2, ";|)|="))
break;
if (Token::Match(tok2, "%var% ("))
break;
if (tok2->varId() &&
!Token::Match(tok2->previous(), "&|::") &&
!Token::simpleMatch(tok2->next(), "="))
{ {
bailOutVar(checks, tok2->varId()); bool foundError;
if (tok2->next()->str() == "[")
foundError = use_array_or_pointer_data(checks, tok2);
else
foundError = use(checks, tok2);
// prevent duplicate error messages
if (foundError)
{
bailOutVar(checks, tok2->varId());
}
} }
} }
} }

View File

@ -1367,6 +1367,13 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); 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 // #1533
checkUninitVar("char a()\n" checkUninitVar("char a()\n"
"{\n" "{\n"