Fixed #4560 (false positive: (error) Uninitialized variable: s)

This commit is contained in:
Daniel Marjamäki 2013-02-11 17:05:59 +01:00
parent 48e194dc56
commit 94f1d34dcb
2 changed files with 13 additions and 3 deletions

View File

@ -1130,9 +1130,9 @@ static void conditionAlwaysTrueOrFalse(const Token *tok, const std::map<unsigned
// always false // always false
if (Token::Match(vartok, "%var% &&|)")) { if (Token::Match(vartok, "%var% &&|)")) {
if (NOT) if (NOT)
*alwaysFalse = bool(it->second == 0);
else
*alwaysFalse = bool(it->second != 0); *alwaysFalse = bool(it->second != 0);
else
*alwaysFalse = bool(it->second == 0);
} else if (Token::Match(vartok, "%var% == %num% &&|)")) { } else if (Token::Match(vartok, "%var% == %num% &&|)")) {
*alwaysFalse = bool(it->second != MathLib::toLongNumber(vartok->strAt(2))); *alwaysFalse = bool(it->second != MathLib::toLongNumber(vartok->strAt(2)));
} else if (Token::Match(vartok, "%var% != %num% &&|)")) { } else if (Token::Match(vartok, "%var% != %num% &&|)")) {
@ -1212,7 +1212,7 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok,
if (tok->str() == "{") { if (tok->str() == "{") {
bool possibleInitIf(number_of_if > 0 || suppressErrors); bool possibleInitIf(number_of_if > 0 || suppressErrors);
bool noreturnIf = false; 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: // bail out for such code:
// if (a) x=0; // conditional initialization // if (a) x=0; // conditional initialization

View File

@ -2266,6 +2266,16 @@ private:
"}"); "}");
ASSERT_EQUALS("", errout.str()); 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 // asm
checkUninitVar2("void f() {\n" checkUninitVar2("void f() {\n"
" int x;\n" " int x;\n"