From f6b201bb3b113870d8e592e803bad49cf2ac21a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 27 Dec 2011 10:18:49 +0100 Subject: [PATCH] Uninitialized variables: Fixed false positives when pointer is dereferenced in unexpanded macro --- lib/checkuninitvar.cpp | 12 ++++++++++-- test/testuninitvar.cpp | 6 ++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index a9868c550..fdb8105ab 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1265,8 +1265,16 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer) const } bool unknown = false; - if (pointer && CheckNullPointer::isPointerDeRef(vartok, unknown)) - return true; + if (pointer && CheckNullPointer::isPointerDeRef(vartok, unknown)) { + // function parameter? + bool functionParameter = false; + if (Token::Match(vartok->tokAt(-2), "%var% (") || vartok->previous()->str() == ",") + functionParameter = true; + + // if this is not a function parameter report this dereference as variable usage + if (!functionParameter) + return true; + } if (Token::Match(vartok->next(), "++|--|%op%")) return true; diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 6ad330068..2054df935 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -1972,6 +1972,12 @@ private: " if (100 == sizeof(i+1));\n" "}"); ASSERT_EQUALS("", errout.str()); + + checkUninitVar2("void f() {\n" + " struct ABC *abc;\n" + " int i = ARRAY_SIZE(abc.a);" + "}"); + ASSERT_EQUALS("", errout.str()); } };