Fixed #4512 (Improve check: Uninitialized variable not detected)

This commit is contained in:
Daniel Marjamki 2013-01-26 08:16:53 +01:00
parent 04ceae7ea7
commit 8738223e82
2 changed files with 10 additions and 0 deletions

View File

@ -1498,6 +1498,10 @@ bool CheckUninitVar::isVariableUsage(const Scope* scope, const Token *vartok, bo
if ((pointer || address) && Token::Match(argStart, "const %type% * %var% [,)]"))
return true;
}
} else if (Token::Match(start->previous(), "if|while|for")) {
// control-flow statement reading the variable "by value"
return true;
}
}
}

View File

@ -2010,6 +2010,12 @@ private:
ASSERT_EQUALS("", errout.str());
// using uninit var in condition
checkUninitVar2("void f(void) {\n"
" int x;\n"
" if (x) { }\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: x\n", errout.str());
checkUninitVar2("void f() {\n"
" int x;\n"
" if (1 == (3 & x)) { }\n"