From 21af64049cb719ce336c8c22728faee7d33bafd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 5 Jan 2011 20:44:04 +0100 Subject: [PATCH] Fixed #2401 (false positive: Uninitialized variable: result) --- lib/checkuninitvar.cpp | 20 +++++++++++++++++--- test/testuninitvar.cpp | 26 +++++++++++++++++--------- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 53cd707c8..83e74f9d7 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -600,7 +600,7 @@ private: { // sizeof/typeof doesn't dereference. A function name that is all uppercase // might be an unexpanded macro that uses sizeof/typeof - if (Token::Match(&tok, "sizeof|typeof (") || isUpper(tok.str())) + if (Token::Match(&tok, "sizeof|typeof (")) return tok.next()->link(); // deallocate pointer @@ -690,8 +690,22 @@ private: { if (Token::Match(tok2->tokAt(-2), "[(,] *") || Token::Match(tok2->next(), ". %var%")) { - if (use_dead_pointer(checks, tok2)) - ExecutionPath::bailOutVar(checks, tok2->varId()); + // find function call.. + const Token *functionCall = tok2; + while (0 != (functionCall = functionCall ? functionCall->previous() : 0)) + { + if (functionCall->str() == "(") + break; + if (functionCall->str() == ")") + functionCall = functionCall->link(); + } + + functionCall = functionCall ? functionCall->previous() : 0; + if (functionCall) + { + if (functionCall->isName() && !isUpper(functionCall->str()) && use_dead_pointer(checks, tok2)) + ExecutionPath::bailOutVar(checks, tok2->varId()); + } } // it is possible that the variable is initialized here diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 95ee51cc1..9a0894ff3 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -637,15 +637,15 @@ private: "}\n"); ASSERT_EQUALS("", errout.str()); - checkUninitVar("int test(int cond1, int cond2) {\n" - " int foo;\n" - " if (cond1 || cond2) {\n" - " if (cond2)\n" - " foo = 0;\n" - " }\n" - " if (cond2) {\n" - " int t = foo*foo;\n" - " }\n" + checkUninitVar("int test(int cond1, int cond2) {\n" + " int foo;\n" + " if (cond1 || cond2) {\n" + " if (cond2)\n" + " foo = 0;\n" + " }\n" + " if (cond2) {\n" + " int t = foo*foo;\n" + " }\n" "}\n"); ASSERT_EQUALS("", errout.str()); @@ -1366,6 +1366,14 @@ private: TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: x\n", errout.str()); ASSERT_EQUALS("", errout.str()); + // #2401 - unknown function/macro might init the variable + checkUninitVar("int f() {\n" + " int x;\n" + " INIT(x);\n" + " return x;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + // using uninitialized function pointer.. checkUninitVar("void foo()\n" "{\n"