Fix #8126 unsafeClassCanLeak missing for array of pointers (#3832)

This commit is contained in:
chrchr-github 2022-02-15 14:28:19 +01:00 committed by GitHub
parent a5674182bb
commit e7e2439347
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 1 deletions

View File

@ -538,7 +538,7 @@ void CheckMemoryLeakInClass::check()
// only check classes and structures
for (const Scope * scope : symbolDatabase->classAndStructScopes) {
for (const Variable &var : scope->varlist) {
if (!var.isStatic() && var.isPointer()) {
if (!var.isStatic() && (var.isPointer() || var.isPointerArray())) {
// allocation but no deallocation of private variables in public function..
const Token *tok = var.typeStartToken();
// Either it is of standard type or a non-derived type

View File

@ -521,6 +521,7 @@ private:
TEST_CASE(class24); // ticket #3806 - false positive in copy constructor
TEST_CASE(class25); // ticket #4367 - false positive implementation for destructor is not seen
TEST_CASE(class26); // ticket #10789
TEST_CASE(class27); // ticket #8126
TEST_CASE(staticvar);
@ -1461,6 +1462,17 @@ private:
ASSERT_EQUALS("[test.cpp:5]: (style) Class 'S' is unsafe, 'S::p' can leak by wrong usage.\n", errout.str());
}
void class27() { // ticket #8126 - array of pointers
check("struct S {\n"
" S() {\n"
" for (int i = 0; i < 1; i++)\n"
" a = new char[3];\n"
" }\n"
" char* a;\n"
"};\n");
ASSERT_EQUALS("[test.cpp:6]: (style) Class 'S' is unsafe, 'S::a' can leak by wrong usage.\n", errout.str());
}
void staticvar() {
check("class A\n"
"{\n"