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,29 +2885,39 @@ private:
return &tok;
}
if (Token::Match(tok.previous(), "[;{}] %var% =|["))
if (Token::Match(tok.previous(), "[;{}] %var% =|[|."))
{
// check variable usages in rhs/index
for (const Token *tok2 = tok.tokAt(2); tok2; tok2 = tok2->next())
if (tok.next()->str() == ".")
{
if (Token::Match(tok2, ";|)|="))
break;
if (Token::Match(tok2, "%var% ("))
break;
if (tok2->varId() &&
!Token::Match(tok2->previous(), "&|::") &&
!Token::simpleMatch(tok2->next(), "="))
if (use_dead_pointer(checks, &tok))
{
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)
return &tok;
}
}
else
{
// check variable usages in rhs/index
for (const Token *tok2 = tok.tokAt(2); tok2; tok2 = tok2->next())
{
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");
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"