From 94f1d34dcb51ec313ec54c4691eb4307846a107e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 11 Feb 2013 17:05:59 +0100 Subject: [PATCH] Fixed #4560 (false positive: (error) Uninitialized variable: s) --- lib/checkuninitvar.cpp | 6 +++--- test/testuninitvar.cpp | 10 ++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index d828533e5..2bfcf7756 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1130,9 +1130,9 @@ static void conditionAlwaysTrueOrFalse(const Token *tok, const std::mapsecond == 0); - else *alwaysFalse = bool(it->second != 0); + else + *alwaysFalse = bool(it->second == 0); } else if (Token::Match(vartok, "%var% == %num% &&|)")) { *alwaysFalse = bool(it->second != MathLib::toLongNumber(vartok->strAt(2))); } else if (Token::Match(vartok, "%var% != %num% &&|)")) { @@ -1212,7 +1212,7 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok, if (tok->str() == "{") { bool possibleInitIf(number_of_if > 0 || suppressErrors); bool noreturnIf = false; - const bool initif = !alwaysFalse && !alwaysTrue && checkScopeForVariable(scope, tok->next(), var, &possibleInitIf, &noreturnIf, membervar); + const bool initif = !alwaysFalse && checkScopeForVariable(scope, tok->next(), var, &possibleInitIf, &noreturnIf, membervar); // bail out for such code: // if (a) x=0; // conditional initialization diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index e47a51e93..7ad683cbe 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -2266,6 +2266,16 @@ private: "}"); ASSERT_EQUALS("", errout.str()); + checkUninitVar2("int* f(int a) {\n" // #4560 + " int x = 0, y;\n" + " if (a) x = 1;\n" + " else return;\n" + " if (x) y = 123;\n" // <- y is always initialized + " else y = 456;\n" + " return y;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + // asm checkUninitVar2("void f() {\n" " int x;\n"