Fix #5678 FN destructor with no definition hide mismatching alloc/dealloc (#4401)

* Fix #5678 FN destructor with no definition hide mismatching allocation / deallocation

* Format

* Fix test

* Format
This commit is contained in:
chrchr-github 2022-08-25 22:52:51 +02:00 committed by GitHub
parent 35fba07910
commit 917496a844
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 4 deletions

View File

@ -632,14 +632,14 @@ void CheckMemoryLeakInClass::variable(const Scope *scope, const Token *tokVarnam
if (destructor)
deallocInDestructor = true;
// several types of allocation/deallocation?
if (memberDealloc != CheckMemoryLeak::No && memberDealloc != dealloc)
dealloc = CheckMemoryLeak::Many;
if (dealloc != CheckMemoryLeak::Many && memberAlloc != CheckMemoryLeak::No && memberAlloc != Many && memberAlloc != dealloc) {
mismatchAllocDealloc({tok}, classname + "::" + varname);
}
// several types of allocation/deallocation?
if (memberDealloc != CheckMemoryLeak::No && memberDealloc != dealloc)
dealloc = CheckMemoryLeak::Many;
memberDealloc = dealloc;
}

View File

@ -1571,6 +1571,18 @@ private:
" delete [] pkt_buffer;\n"
"}");
ASSERT_EQUALS("[test.cpp:14]: (error) Mismatching allocation and deallocation: A::pkt_buffer\n", errout.str());
check("struct S {\n" // 5678
" ~S();\n"
" void f();\n"
" int* p;\n"
"};\n"
"void S::f() {\n"
" p = new char[1];\n"
" delete p;\n"
" p = 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:8]: (error) Mismatching allocation and deallocation: S::p\n", errout.str());
}
void mismatch2() { // #5659