Skip to content

Commit

Permalink
Partial fix for #12062 FN: memleak (#5548)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github authored Oct 13, 2023
1 parent f56677a commit efd488b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
20 changes: 13 additions & 7 deletions lib/checkleakautovar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ static const int NEW = -1;
static const std::array<std::pair<std::string, std::string>, 4> alloc_failed_conds {{{"==", "0"}, {"<", "0"}, {"==", "-1"}, {"<=", "-1"}}};
static const std::array<std::pair<std::string, std::string>, 4> alloc_success_conds {{{"!=", "0"}, {">", "0"}, {"!=", "-1"}, {">=", "0"}}};

static bool isAutoDeallocType(const Type* type) {
if (!type)
return true;
if (type->classScope && type->classScope->numConstructors == 0 &&
(type->classScope->varlist.empty() || type->needInitialization == Type::NeedInitialization::True) &&
std::none_of(type->derivedFrom.cbegin(), type->derivedFrom.cend(), [](const Type::BaseInfo& bi) {
return isAutoDeallocType(bi.type);
}))
return false;
return true;
}

/**
* @brief Is variable type some class with automatic deallocation?
* @param var variable token
Expand All @@ -72,14 +84,8 @@ static bool isAutoDealloc(const Variable *var)

// return false if the type is a simple record type without side effects
// a type that has no side effects (no constructors and no members with constructors)
/** @todo false negative: check base class for side effects */
/** @todo false negative: check constructors for side effects */
if (var->typeScope() && var->typeScope()->numConstructors == 0 &&
(var->typeScope()->varlist.empty() || var->type()->needInitialization == Type::NeedInitialization::True) &&
var->type()->derivedFrom.empty())
return false;

return true;
return isAutoDeallocType(var->type());
}

template<std::size_t N>
Expand Down
8 changes: 8 additions & 0 deletions test/testleakautovar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,14 @@ class TestLeakAutoVar : public TestFixture {
" b = Bar(*new Foo(data));\n"
"}", /*cpp*/ true);
ASSERT_EQUALS("[test.cpp:4]: (information) --check-library: Function Foo() should have <use>/<leak-ignore> configuration\n", errout.str());

check("class B {};\n"
" class D : public B {};\n"
" void g() {\n"
" auto d = new D();\n"
" if (d) {}\n"
"}", /*cpp*/ true);
ASSERT_EQUALS("[test.cpp:6]: (error) Memory leak: d\n", errout.str());
}

void realloc1() {
Expand Down

0 comments on commit efd488b

Please sign in to comment.