checkuninitvar: Fix FN with c++ casts (#2549)

This commit is contained in:
Rikard Falkeborn 2020-02-23 19:53:17 +01:00 committed by GitHub
parent 4f9a0b8420
commit 9896dce7f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 1 deletions

View File

@ -713,7 +713,7 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var
if (var.isPointer() && Token::simpleMatch(tok->next(), "=")) { if (var.isPointer() && Token::simpleMatch(tok->next(), "=")) {
const Token *rhs = tok->next()->astOperand2(); const Token *rhs = tok->next()->astOperand2();
while (rhs && rhs->isCast()) while (rhs && rhs->isCast())
rhs = rhs->astOperand1(); rhs = rhs->astOperand2() ? rhs->astOperand2() : rhs->astOperand1();
if (rhs && Token::Match(rhs->previous(), "%name% (") && if (rhs && Token::Match(rhs->previous(), "%name% (") &&
mSettings->library.returnuninitdata.count(rhs->previous()->str()) > 0U) { mSettings->library.returnuninitdata.count(rhs->previous()->str()) > 0U) {
*alloc = NO_CTOR_CALL; *alloc = NO_CTOR_CALL;

View File

@ -1478,6 +1478,14 @@ private:
"}", "test.cpp", false); "}", "test.cpp", false);
ASSERT_EQUALS("[test.cpp:4]: (error) Memory is allocated but not initialized: buffer\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (error) Memory is allocated but not initialized: buffer\n", errout.str());
checkUninitVar("void f(){\n"
" char *strMsg = \"This is a message\";\n"
" char *buffer=static_cast<char*>(malloc(128*sizeof(char)));\n"
" strcpy(strMsg,buffer);\n"
" free(buffer);\n"
"}", "test.cpp", false);
ASSERT_EQUALS("[test.cpp:4]: (error) Memory is allocated but not initialized: buffer\n", errout.str());
// #3845 // #3845
checkUninitVar("int foo() {\n" checkUninitVar("int foo() {\n"
" int a[1] = {5};\n" " int a[1] = {5};\n"