From c37b8ea55c8180f0d7c94c3cfd4dc3a2720b651a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 10 May 2021 18:38:44 +0200 Subject: [PATCH] Fixed #8299 (false negative: uninitialized struct member) --- lib/checkuninitvar.cpp | 2 ++ test/testuninitvar.cpp | 11 ++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 4c0735021..ae31228c7 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -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 diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index ed1184235..08414732d 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -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"