From 8cc1f664d8d1c8352eadba359fa2c46f4d9f96aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 10 May 2014 20:20:55 +0200 Subject: [PATCH] Uninitialized member variable: Fixed false negative when struct is read --- lib/checkuninitvar.cpp | 11 +++++++++++ test/testuninitvar.cpp | 8 ++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 88b1afb60..b7be4a5f0 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1872,6 +1872,9 @@ bool CheckUninitVar::isMemberVariableAssignment(const Token *tok, const std::str if (Token::Match(argStart, "const struct| %type% * const| %var% [,)]")) return false; } + + else if (Token::Match(ftok ? ftok->previous() : nullptr, "= * (")) + return false; } return true; } @@ -1891,6 +1894,14 @@ bool CheckUninitVar::isMemberVariableUsage(const Token *tok, bool isPointer, boo else if (!isPointer && Token::Match(tok->previous(), "= %var% ;")) return true; + // = *(&var); + else if (!isPointer && + Token::simpleMatch(tok->astParent(),"&") && + Token::simpleMatch(tok->astParent()->astParent(),"*") && + Token::Match(tok->astParent()->astParent()->astParent(), "= * (| &") && + tok->astParent()->astParent()->astParent()->astOperand2() == tok->astParent()->astParent()) + return true; + else if (_settings->experimental && !isPointer && Token::Match(tok->tokAt(-2), "[(,] & %var% [,)]") && diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 5de982251..521286408 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -2917,6 +2917,14 @@ private: "}\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" + "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"