Uninitialized variables: Fixed false positive for '*p=..'

This commit is contained in:
Daniel Marjamäki 2013-12-12 15:33:31 +01:00
parent 7321f92eff
commit 35189e80f2
4 changed files with 17 additions and 2 deletions

View File

@ -1742,7 +1742,13 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, bool all
}
if (vartok->previous()->str() != "&" || !Token::Match(vartok->tokAt(-2), "[(,=?:]")) {
return (pointer && alloc && vartok->strAt(-1) != "*") ? false : true;
if (alloc && vartok->previous()->str() == "*") {
const Token *parent = vartok->previous()->astParent();
if (parent && parent->str() == "=" && parent->astOperand1() == vartok->previous())
return false;
return true;
}
return !alloc;
}
}

View File

@ -662,6 +662,9 @@ public:
const Token * astOperand2() const {
return _astOperand2;
}
const Token * astParent() const {
return _astParent;
}
const Token *astTop() const {
const Token *ret = this;
while (ret->_astParent)

View File

@ -667,7 +667,7 @@ static void compileExpression(Token *&tok, std::stack<Token*> &op)
void TokenList::createAst()
{
for (Token *tok = _front; tok; tok = tok ? tok->next() : NULL) {
if (tok->str() == "return" || !tok->previous() || Token::Match(tok, "%var% (|[|.|=")) {
if (tok->str() == "return" || !tok->previous() || Token::Match(tok, "%var% (|[|.|=") || Token::Match(tok->previous(), "[;{}] %cop%")) {
std::stack<Token *> operands;
compileExpression(tok, operands);
}

View File

@ -3278,6 +3278,12 @@ private:
"}\n");
ASSERT_EQUALS("", errout.str());
checkUninitVar2("void f() {\n"
" char *s = malloc(100);\n"
" *s = x;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
// function parameter (treat it as initialized until malloc is used)
checkUninitVar2("int f(int *p) {\n"
" if (*p == 1) {}\n" // no error