From 5f1fadec7b0069d7be8d02591431a7e5ceb61a54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 16 Dec 2011 19:56:13 +0100 Subject: [PATCH] Uninitialized variables: Fixed false positives when many 'if' are used. Ticket: #3369 --- lib/checkuninitvar.cpp | 3 +++ test/testuninitvar.cpp | 14 +++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 8912377e1..d5d0e8290 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1062,6 +1062,9 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const unsigned int for (; tok; tok = tok->next()) { // End of scope.. if (tok->str() == "}") { + if (number_of_if) + return true; + // might be a noreturn function.. if (Token::simpleMatch(tok->tokAt(-2), ") ; }") && Token::Match(tok->linkAt(-2)->tokAt(-2), "[;{}] %var% (") && diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 6baaca276..488a28fde 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -1753,7 +1753,7 @@ private: " else if (y == 2) { x = 1; }\n" " return x;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: x\n", errout.str()); + TODO_ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: x\n", "", errout.str()); // initialization in condition checkUninitVar2("void f() {\n" @@ -1839,6 +1839,18 @@ private: "}"); ASSERT_EQUALS("", errout.str()); + checkUninitVar2("void f() {\n" + " int a,b=0;\n" + " if (x) {\n" + " if (y) {\n" + " a = 0;\n" + " b = 1;\n" + " }\n" + " }\n" + " if (b) a++;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + // asm checkUninitVar2("void f() {\n" " int x;\n"