From 4c344adcf1d0e5e21acf559e62a7705f7a4372f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 10 May 2014 19:56:44 +0200 Subject: [PATCH] Uninitialized struct member: fixed false negative when struct assignment is seen --- lib/checkuninitvar.cpp | 3 +++ test/testuninitvar.cpp | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index f8dc0e227..88b1afb60 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1888,6 +1888,9 @@ bool CheckUninitVar::isMemberVariableUsage(const Token *tok, bool isPointer, boo else if (!isPointer && Token::Match(tok->previous(), "[(,] %var% [,)]") && isVariableUsage(tok, isPointer, alloc, _tokenizer->isCPP())) return true; + else if (!isPointer && Token::Match(tok->previous(), "= %var% ;")) + return true; + else if (_settings->experimental && !isPointer && Token::Match(tok->tokAt(-2), "[(,] & %var% [,)]") && diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 89d19c5cb..5de982251 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -2909,6 +2909,14 @@ private: "}\n", "test.c"); ASSERT_EQUALS("[test.c:4]: (error) Uninitialized struct member: ab.a\n", errout.str()); + checkUninitVar2("struct AB { int a; int b; };\n" + "void f(void) {\n" + " struct AB ab;\n" + " ab.a = 1;\n" + " x = ab;\n" + "}\n", "test.c"); + ASSERT_EQUALS("[test.c:5]: (error) Uninitialized struct member: ab.b\n", errout.str()); + checkUninitVar2("struct AB { int a; int b; };\n" // pass struct member by address "void f(void) {\n" " struct AB ab;\n"