diff --git a/src/checkmemoryleak.cpp b/src/checkmemoryleak.cpp index 8e70a41ea..cc12918f8 100644 --- a/src/checkmemoryleak.cpp +++ b/src/checkmemoryleak.cpp @@ -419,13 +419,17 @@ Token *CheckMemoryLeakClass::getcode(const Token *tok, std::list { if (! realloc) addtoken("alloc"); - if ((alloctype != No && alloctype != alloc) || - (dealloctype != No && dealloctype != alloc)) + + if (alloctype != No && alloctype != alloc) + alloc = Many; + + if (alloc != Many && dealloctype != No && dealloctype != Many && dealloctype != alloc) { callstack.push_back(tok); ErrorMessage::mismatchAllocDealloc(_errorLogger, _tokenizer, callstack, varname); callstack.pop_back(); } + alloctype = alloc; } @@ -462,8 +466,11 @@ Token *CheckMemoryLeakClass::getcode(const Token *tok, std::list if (dealloc != No) { addtoken("dealloc"); - if ((alloctype != No && alloctype != dealloc) || - (dealloctype != No && dealloctype != dealloc)) + + if (dealloctype != No && dealloctype != dealloc) + dealloc = Many; + + if (dealloc != Many && alloctype != No && alloctype != Many && alloctype != dealloc) { callstack.push_back(tok); ErrorMessage::mismatchAllocDealloc(_errorLogger, _tokenizer, callstack, varname); @@ -1457,14 +1464,17 @@ void CheckMemoryLeakClass::CheckMemoryLeak_ClassMembers_Variable(const std::vect AllocType alloc = GetAllocationType(tok->tokAt(2)); if (alloc != No) { + if (Alloc != No && Alloc != alloc) + alloc = Many; + std::list callstack; - if ((Dealloc != No && Dealloc != alloc) || - (Alloc != No && Alloc != alloc)) + if (alloc != Many && Dealloc != No && Dealloc != Many && Dealloc != alloc) { callstack.push_back(tok); ErrorMessage::mismatchAllocDealloc(_errorLogger, _tokenizer, callstack, FullVariableName.str()); callstack.pop_back(); } + Alloc = alloc; } } @@ -1481,14 +1491,17 @@ void CheckMemoryLeakClass::CheckMemoryLeak_ClassMembers_Variable(const std::vect } if (dealloc != No) { + if (Dealloc != No && Dealloc != dealloc) + dealloc = Many; + std::list callstack; - if ((Dealloc != No && Dealloc != dealloc) || - (Alloc != No && Alloc != dealloc)) + if (dealloc != Many && Alloc != No && Alloc != Many && Alloc != dealloc) { callstack.push_back(tok); ErrorMessage::mismatchAllocDealloc(_errorLogger, _tokenizer, callstack, FullVariableName.str()); callstack.pop_back(); } + Dealloc = dealloc; } diff --git a/src/checkmemoryleak.h b/src/checkmemoryleak.h index 16909529a..93b254b75 100644 --- a/src/checkmemoryleak.h +++ b/src/checkmemoryleak.h @@ -41,7 +41,8 @@ public: private: - enum AllocType { No, Malloc, gMalloc, New, NewArray, FOPEN, POPEN }; + /** What type of allocation are used.. the "Many" means that several types of allocation and deallocation are used */ + enum AllocType { No, Malloc, gMalloc, New, NewArray, FOPEN, POPEN, Many }; // Extra allocation.. class AllocFunc diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 85738db8b..03f2c285d 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -120,8 +120,8 @@ private: TEST_CASE(ret5); // Bug 2458436 - return use TEST_CASE(mismatch1); - // TODO TEST_CASE(mismatch2); - // TODO TEST_CASE(mismatch3); + TEST_CASE(mismatch2); + TEST_CASE(mismatch3); TEST_CASE(mismatch4); TEST_CASE(func1); @@ -1013,8 +1013,7 @@ private: " }\n" " delete [] p;\n" "}\n", false); - ASSERT_EQUALS(std::string("[test.cpp:6]: (all) Mismatching allocation and deallocation: p\n" - "[test.cpp:9]: (all) Mismatching allocation and deallocation: p\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:6]: (all) Mismatching allocation and deallocation: p\n"), errout.str()); }