Fix false positive memoryleak (#2882)

```
int *f() {
  int *p = static_cast<int *>(realloc(nullptr, 10));
  if (!!(!p)) {
    return nullptr;
  }
  return p;
}

```
would give
```
memleak2.cpp:4:5: error: Memory leak: p [memleak]
    return nullptr;
    ^
```
Because of the additional `!!̀ .
This commit is contained in:
Ken-Patrick Lehrmann 2020-11-10 15:59:51 +01:00 committed by GitHub
parent 59c189752b
commit 79bdd64689
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 1 deletions

View File

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

View File

@ -138,6 +138,7 @@ private:
TEST_CASE(ifelse14); // #9130 - if (x == (char*)NULL)
TEST_CASE(ifelse15); // #9206 - if (global_ptr = malloc(1))
TEST_CASE(ifelse16); // #9635 - if (p = malloc(4), p == NULL)
TEST_CASE(ifelse17); // if (!!(!p))
// switch
TEST_CASE(switch1);
@ -1517,6 +1518,24 @@ private:
ASSERT_EQUALS("", errout.str());
}
void ifelse17() {
check("int *f() {\n"
" int *p = realloc(nullptr, 10);\n"
" if (!p)\n"
" return NULL;\n"
" return p;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("int *f() {\n"
" int *p = realloc(nullptr, 10);\n"
" if (!!(!p))\n"
" return NULL;\n"
" return p;\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void switch1() {
check("void f() {\n"
" char *p = 0;\n"