Fixed #8794 - Memory leak false positive triggered by !(x != NULL) (#1450)

This commit is contained in:
Kamil Dudka 2018-10-26 06:21:45 +02:00 committed by Daniel Marjamäki
parent 86782af43c
commit d206047b84
2 changed files with 15 additions and 1 deletions

View File

@ -121,8 +121,11 @@ const Token * astIsVariableComparison(const Token *tok, const std::string &comp,
} else if (comp == "!=" && rhs == std::string("0")) {
ret = tok;
} else if (comp == "==" && rhs == std::string("0")) {
if (tok->str() == "!")
if (tok->str() == "!") {
ret = tok->astOperand1();
// handle (!(x!=0)) as (x==0)
astIsVariableComparison(ret, "!=", "0", &ret);
}
}
while (ret && ret->str() == ".")
ret = ret->astOperand2();

View File

@ -98,6 +98,7 @@ private:
TEST_CASE(ifelse7); // #5576 - if (fd < 0)
TEST_CASE(ifelse8); // #5747 - if (fd == -1)
TEST_CASE(ifelse9); // #5273 - if (X(p==NULL, 0))
TEST_CASE(ifelse10); // #8794 - if (!(x!=NULL))
// switch
TEST_CASE(switch1);
@ -1150,6 +1151,16 @@ private:
ASSERT_EQUALS("", errout.str());
}
void ifelse10() { // #8794
check("void f() {\n"
" void *x = malloc(1U);\n"
" if (!(x != NULL))\n"
" return;\n"
" free(x);\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void switch1() {
check("void f() {\n"
" char *p = 0;\n"