Uninitialized variables: Fixed false positive for '*p=..'
This commit is contained in:
parent
7321f92eff
commit
35189e80f2
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue