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"