Fixed #9422 (FP uninitdata - writing pointer to output stream)

This commit is contained in:
Daniel Marjamäki 2019-11-16 11:47:57 +01:00
parent 7f57c980f0
commit bae4faa786
2 changed files with 34 additions and 10 deletions

View File

@ -1093,16 +1093,7 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, Alloc al
}
if (mTokenizer->isCPP() && Token::simpleMatch(vartok->next(), "<<")) {
// Is this calculation done in rhs?
const Token *tok = vartok;
while (Token::Match(tok, "%name%|.|::"))
tok = tok->previous();
if (Token::Match(tok, "[;{}]"))
return false;
// Is variable a known POD type then this is a variable usage,
// otherwise we assume it's not.
return (vartok->valueType() && vartok->valueType()->isIntegral());
return false;
}
if (alloc == NO_ALLOC && vartok->next() && vartok->next()->isOp() && !vartok->next()->isAssignmentOp())

View File

@ -475,6 +475,39 @@ private:
" os << 1 << a;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());
{
// #9422
checkUninitVar("void f() {\n"
" char *p = new char[10];\n"
" std::cout << (void *)p << 1;\n"
"}");
ASSERT_EQUALS("", errout.str());
checkUninitVar("void f() {\n"
" char p[10];\n"
" std::cout << (void *)p << 1;\n"
"}");
ASSERT_EQUALS("", errout.str());
checkUninitVar("void f() {\n"
" char *p = new char[10];\n"
" std::cout << p << 1;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Memory is allocated but not initialized: p\n", errout.str());
checkUninitVar("void f() {\n"
" char p[10];\n"
" std::cout << p << 1;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: p\n", errout.str());
checkUninitVar("void f() {\n"
" char p[10];\n"
" std::cout << *p << 1;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: p\n", errout.str());
}
}
// #8494 : Overloaded & operator