Fixed #8365 (False positive on memory leak when assigned inside if statement)

This commit is contained in:
Daniel Marjamäki 2019-01-08 20:53:53 +01:00
parent 0aa4910244
commit 0f9ce5edd5
2 changed files with 14 additions and 1 deletions

View File

@ -167,7 +167,9 @@ const Token * astIsVariableComparison(const Token *tok, const std::string &comp,
}
while (ret && ret->str() == ".")
ret = ret->astOperand2();
if (ret && ret->varId() == 0U)
if (ret && ret->str() == "=" && ret->astOperand1() && ret->astOperand1()->varId())
ret = ret->astOperand1();
else if (ret && ret->varId() == 0U)
ret = nullptr;
if (vartok)
*vartok = ret;

View File

@ -99,6 +99,7 @@ private:
TEST_CASE(ifelse8); // #5747 - if (fd == -1)
TEST_CASE(ifelse9); // #5273 - if (X(p==NULL, 0))
TEST_CASE(ifelse10); // #8794 - if (!(x!=NULL))
TEST_CASE(ifelse11); // #8365 - if (NULL == (p = malloc(4)))
// switch
TEST_CASE(switch1);
@ -1161,6 +1162,16 @@ private:
ASSERT_EQUALS("", errout.str());
}
void ifelse11() { // #8365
check("void f() {\n"
" void *p;\n"
" if (NULL == (p = malloc(4)))\n"
" return;\n"
" free(p);\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void switch1() {
check("void f() {\n"
" char *p = 0;\n"