Skip to content

Commit 917496a

Browse files
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
1 parent 35fba07 commit 917496a

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

lib/checkmemoryleak.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -632,14 +632,14 @@ void CheckMemoryLeakInClass::variable(const Scope *scope, const Token *tokVarnam
632632
if (destructor)
633633
deallocInDestructor = true;
634634

635-
// several types of allocation/deallocation?
636-
if (memberDealloc != CheckMemoryLeak::No && memberDealloc != dealloc)
637-
dealloc = CheckMemoryLeak::Many;
638-
639635
if (dealloc != CheckMemoryLeak::Many && memberAlloc != CheckMemoryLeak::No && memberAlloc != Many && memberAlloc != dealloc) {
640636
mismatchAllocDealloc({tok}, classname + "::" + varname);
641637
}
642638

639+
// several types of allocation/deallocation?
640+
if (memberDealloc != CheckMemoryLeak::No && memberDealloc != dealloc)
641+
dealloc = CheckMemoryLeak::Many;
642+
643643
memberDealloc = dealloc;
644644
}
645645

test/testmemleak.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,6 +1571,18 @@ class TestMemleakInClass : public TestFixture {
15711571
" delete [] pkt_buffer;\n"
15721572
"}");
15731573
ASSERT_EQUALS("[test.cpp:14]: (error) Mismatching allocation and deallocation: A::pkt_buffer\n", errout.str());
1574+
1575+
check("struct S {\n" // 5678
1576+
" ~S();\n"
1577+
" void f();\n"
1578+
" int* p;\n"
1579+
"};\n"
1580+
"void S::f() {\n"
1581+
" p = new char[1];\n"
1582+
" delete p;\n"
1583+
" p = 0;\n"
1584+
"}\n");
1585+
ASSERT_EQUALS("[test.cpp:8]: (error) Mismatching allocation and deallocation: S::p\n", errout.str());
15741586
}
15751587

15761588
void mismatch2() { // #5659

0 commit comments

Comments
 (0)