From 1b20f8d27d72ebfa1e3be08f6df8be530ca8a8c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 6 Jul 2010 13:18:28 +0200 Subject: [PATCH] Fixed #1824 (false positive: unitialised variable) --- lib/executionpath.cpp | 31 +++++++++++++++++-------------- test/testother.cpp | 23 +++++++++++++++++++++-- 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/lib/executionpath.cpp b/lib/executionpath.cpp index e576d6e15..5c5200e81 100644 --- a/lib/executionpath.cpp +++ b/lib/executionpath.cpp @@ -42,26 +42,28 @@ bool ExecutionPath::parseCondition(const Token &tok, std::list break; --parlevel; } - else if (Token::Match(tok2, ";{}")) + else if (Token::Match(tok2, "[;{}]")) break; if (tok2->varId() != 0) { bailOutVar(checks, tok2->varId()); - for (std::list::iterator it = checks.begin(); it != checks.end();) - { - if ((*it)->varId > 0 && (*it)->numberOfIf >= 1) - { - delete *it; - checks.erase(it++); - } - else - { - ++it; - } - } } } + for (std::list::iterator it = checks.begin(); it != checks.end();) + { + if ((*it)->varId > 0 && (*it)->numberOfIf >= 1) + { + delete *it; + checks.erase(it++); + } + else + { + ++it; + } + } + + return false; } @@ -103,7 +105,8 @@ static void parseIfSwitchBody(const Token * const tok, std::list::const_iterator it; for (it = checks.begin(); it != checks.end(); ++it) { - c.push_back((*it)->copy()); + if ((*it)->numberOfIf == 0) + c.push_back((*it)->copy()); if ((*it)->varId != 0) countif2.insert((*it)->varId); } diff --git a/test/testother.cpp b/test/testother.cpp index 926806105..cfb14ab5d 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -1174,6 +1174,7 @@ private: Tokenizer tokenizer; std::istringstream istr(code); tokenizer.tokenize(istr, "test.cpp"); + tokenizer.simplifyTokenList(); // Clear the error buffer.. errout.str(""); @@ -1351,10 +1352,10 @@ private: "}\n"); ASSERT_EQUALS("", errout.str()); - checkUninitVar("void a()\n" + checkUninitVar("int a()\n" "{\n" " int x;\n" - " int y = x;\n" + " return x;\n" "}\n"); ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: x\n", errout.str()); @@ -1617,6 +1618,24 @@ private: "}\n"); ASSERT_EQUALS("", errout.str()); + checkUninitVar("void foo(int a)\n" + "{\n" + " int n;\n" + " int condition;\n" + " if(a == 1) {\n" + " n=0;\n" + " condition=0;\n" + " }\n" + " else {\n" + " n=1;\n" + " }\n" + "\n" + " if( n == 0) {\n" + " a=condition;\n" + " }\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + checkUninitVar("void f()\n" "{\n" " C *c;\n"