From ab7ffd2391da4c55db49a2bf1c8feafeaf1df5e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 26 Dec 2009 16:21:53 +0100 Subject: [PATCH] Uninitialized variables: Fixed false positive for 'if (ptr)' --- lib/checkother.cpp | 9 ++++++++- test/testother.cpp | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 43f7c6f2a..af6fe1bc4 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1419,6 +1419,8 @@ private: continue; if (mode == 2 && !c->pointer) continue; + if (mode == 3 && (c->pointer && c->alloc)) + continue; CheckOther *checkOther = dynamic_cast(c->owner); if (checkOther) @@ -1450,6 +1452,11 @@ private: use(foundError, checks, tok, 2); } + static void use_not_pointer(bool &foundError, std::list &checks, const Token *tok) + { + use(foundError, checks, tok, 3); + } + const Token *parse(const Token &tok, bool &foundError, std::list &checks) const { // Variable declaration.. @@ -1650,7 +1657,7 @@ private: bool foundError = false; if (tok.varId() && Token::Match(&tok, "%var% <|<=|==|!=|)|[")) - use(foundError, checks, &tok); + use_not_pointer(foundError, checks, &tok); else if (Token::Match(&tok, "!| %var% (")) { diff --git a/test/testother.cpp b/test/testother.cpp index 23b4b369c..0ea7f141d 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -1314,6 +1314,20 @@ private: "}\n"); ASSERT_EQUALS("", errout.str()); + checkUninitVar("void foo()\n" + "{\n" + " char *a;\n" + " if (a);\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: a\n", errout.str()); + + checkUninitVar("void foo()\n" + "{\n" + " char *a = malloc(100);\n" + " if (a);\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + // struct.. checkUninitVar("void f()\n" "{\n"