Skip to content

Commit

Permalink
Fixed #8299 (false negative: uninitialized struct member)
Browse files Browse the repository at this point in the history
  • Loading branch information
danmar committed May 10, 2021
1 parent 271acf8 commit c37b8ea
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/checkuninitvar.cpp
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion test/testuninitvar.cpp
Expand Up @@ -758,7 +758,8 @@ class TestUninitVar : public TestFixture {
" 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());

Expand Down Expand Up @@ -3511,6 +3512,14 @@ class TestUninitVar : public TestFixture {
"}\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"
Expand Down

0 comments on commit c37b8ea

Please sign in to comment.