From db2c36260458566b6570b81da55ab9358d5de3b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 17 Feb 2010 18:10:50 +0100 Subject: [PATCH] Fixed #1389 (false positive: uninitialized variable) --- lib/executionpath.cpp | 3 +++ test/testother.cpp | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/lib/executionpath.cpp b/lib/executionpath.cpp index 22602471c..0e9411c5d 100644 --- a/lib/executionpath.cpp +++ b/lib/executionpath.cpp @@ -113,9 +113,12 @@ static const Token *checkExecutionPaths_(const Token *tok, std::listtokAt(2)->link(); + // bail out all variables if the scope contains a "return" // bail out all variables used in this for/while/switch/do for (; tok && tok != tok2; tok = tok->next()) { + if (tok->str() == "return") + ExecutionPath::bailOut(checks); if (tok->varId()) ExecutionPath::bailOutVar(checks, tok->varId()); } diff --git a/test/testother.cpp b/test/testother.cpp index 09884515f..e8b4c2488 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -1462,6 +1462,30 @@ private: "}\n"); ASSERT_EQUALS("", errout.str()); + checkUninitVar("int foo(const int iVar, unsigned int slot, unsigned int pin)\n" + "{\n" + " int i;\n" + "\n" + " if (iVar == 0)\n" + " {\n" + " switch (slot)\n" + " {\n" + " case 4: return 5;\n" + " default: return -1;\n" + " }\n" + " }\n" + " else\n" + " {\n" + " switch (pin)\n" + " {\n" + " case 0: i = 2; break;\n" + " default: i = -1; break;\n" + " }\n" + " }\n" + " return i;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + // while.. checkUninitVar("int f()\n" "{\n"