Fixed #4903 (Improve check: allocated but not initialized (condition))

This commit is contained in:
Daniel Marjamäki 2018-01-02 23:19:33 +01:00
parent 95d8534d89
commit 255b788d4d
2 changed files with 9 additions and 2 deletions

View File

@ -522,8 +522,8 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var
else if (Token::Match(tok, "for|while (") || Token::simpleMatch(tok, "do {")) {
const bool forwhile = Token::Match(tok, "for|while (");
// is variable initialized in for-head (don't report errors yet)?
if (forwhile && checkIfForWhileHead(tok->next(), var, true, false, *alloc, membervar))
// is variable initialized in for-head?
if (forwhile && checkIfForWhileHead(tok->next(), var, tok->str() == "for", false, *alloc, membervar))
return true;
// goto the {

View File

@ -3595,6 +3595,13 @@ private:
"}");
ASSERT_EQUALS("", errout.str());
checkUninitVar("int f() {\n"
" char *p = (char *)malloc(256);\n"
" while(*p && *p == '_')\n"
" p++;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Memory is allocated but not initialized: p\n", errout.str());
// #6646 - init in for loop
checkUninitVar("void f() {\n" // No FP
" for (int i;;i++)\n"