Fixed #5259 (Improve check: Uninitialized variable not reported when used in array initialization)

This commit is contained in:
Daniel Marjamäki 2019-08-24 14:43:35 +02:00
parent 5c488b9519
commit 9d26be8380
3 changed files with 27 additions and 1 deletions

View File

@ -543,6 +543,25 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var
return true;
}
const Token *errorToken = nullptr;
visitAstNodes(tok->next(),
[&](const Token *child) {
if (child->isUnaryOp("&"))
return ChildrenToVisit::none;
if (child->str() == "," || child->str() == "{" || child->isConstOp())
return ChildrenToVisit::op1_and_op2;
if (child->str() == "." && Token::Match(child->astOperand1(), "%varid", var.declarationId()) && child->astOperand2() && child->astOperand2()->str() == membervar) {
errorToken = child;
return ChildrenToVisit::done;
}
return ChildrenToVisit::none;
});
if (errorToken) {
uninitStructMemberError(errorToken->astOperand2(), errorToken->astOperand1()->str() + "." + membervar);
return true;
}
// Skip block
tok = end;
continue;

View File

@ -1627,7 +1627,7 @@ void Token::printValueFlow(bool xml, std::ostream &out) const
break;
}
if (value.indirect > 0)
for(int i=0;i<value.indirect;i++)
for (int i=0; i<value.indirect; i++)
out << "*";
}
}

View File

@ -3074,6 +3074,13 @@ private:
"}\n", "test.c");
ASSERT_EQUALS("[test.c:4]: (error) Uninitialized struct member: ab.a\n", errout.str());
checkUninitVar("struct AB { int a; int b; };\n"
"void f(void) {\n"
" AB ab1;\n"
" AB ab2 = { ab1.a, 0 };\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized struct member: ab1.a\n", errout.str());
checkUninitVar("struct AB { int a; int b; };\n"
"void f(void) {\n"
" struct AB ab;\n"