memleak: removed false positives for mismatching allocation and deallocation

This commit is contained in:
Daniel Marjamäki 2009-02-06 06:11:47 +00:00
parent a06861948b
commit 7299d3e5b1
3 changed files with 26 additions and 13 deletions

View File

@ -419,13 +419,17 @@ Token *CheckMemoryLeakClass::getcode(const Token *tok, std::list<const Token *>
{
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<const Token *>
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<const Token *> 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<const Token *> 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;
}

View File

@ -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

View File

@ -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());
}