Fixed #9810 (Improve check: uninitialized struct member not detected)

This commit is contained in:
Daniel Marjamäki 2021-05-10 19:24:03 +02:00
parent c37b8ea55c
commit db7be3e91b
2 changed files with 9 additions and 2 deletions

View File

@ -671,9 +671,9 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var
// variable is seen.. // variable is seen..
if (tok->varId() == var.declarationId()) { if (tok->varId() == var.declarationId()) {
if (!membervar.empty()) { if (!membervar.empty()) {
if (!suppressErrors && Token::Match(tok, "%name% . %name% ;|%cop%") && tok->strAt(2) == membervar) if (!suppressErrors && Token::Match(tok, "%name% . %name%") && tok->strAt(2) == membervar && Token::Match(tok->next()->astParent(), "%cop%|return|throw|?"))
uninitStructMemberError(tok, tok->str() + "." + membervar); uninitStructMemberError(tok, tok->str() + "." + membervar);
else if (mTokenizer->isCPP() && !suppressErrors && Token::Match(tok, "%name% ;")) else if (mTokenizer->isCPP() && !suppressErrors && Token::Match(tok, "%name%") && Token::Match(tok->astParent(), "return|throw|?"))
uninitStructMemberError(tok, tok->str() + "." + membervar); uninitStructMemberError(tok, tok->str() + "." + membervar);
} }

View File

@ -3520,6 +3520,13 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized struct member: s.b\n", errout.str()); ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized struct member: s.b\n", errout.str());
checkUninitVar("struct S { int a; int b; };\n" // #9810
"void f(void) {\n"
" struct S s;\n"
" return s.a ? 1 : 2;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized struct member: s.a\n", errout.str());
// checkIfForWhileHead // checkIfForWhileHead
checkUninitVar("struct FRED {\n" checkUninitVar("struct FRED {\n"
" int a;\n" " int a;\n"