Fixed #8299 (false negative: uninitialized struct member)

This commit is contained in:
Daniel Marjamäki 2021-05-10 18:38:44 +02:00
parent 271acf8aee
commit c37b8ea55c
2 changed files with 12 additions and 1 deletions

View File

@ -673,6 +673,8 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var
if (!membervar.empty()) {
if (!suppressErrors && Token::Match(tok, "%name% . %name% ;|%cop%") && tok->strAt(2) == membervar)
uninitStructMemberError(tok, tok->str() + "." + membervar);
else if (mTokenizer->isCPP() && !suppressErrors && Token::Match(tok, "%name% ;"))
uninitStructMemberError(tok, tok->str() + "." + membervar);
}
// Use variable

View File

@ -758,7 +758,8 @@ private:
" return ab;\n"
"}";
checkUninitVar(code2, "test.cpp");
ASSERT_EQUALS("", errout.str());
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized struct member: ab.a\n"
"[test.cpp:4]: (error) Uninitialized struct member: ab.b\n", errout.str());
checkUninitVar(code2, "test.c");
ASSERT_EQUALS("[test.c:4]: (error) Uninitialized variable: ab\n", errout.str());
@ -3511,6 +3512,14 @@ private:
"}\n", "test.c");
ASSERT_EQUALS("", errout.str());
checkUninitVar("struct S { int a; int b; };\n" // #8299
"void f(void) {\n"
" struct S s;\n"
" s.a = 0;\n"
" return s;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized struct member: s.b\n", errout.str());
// checkIfForWhileHead
checkUninitVar("struct FRED {\n"
" int a;\n"