Memory leaks: Made the checking a lot more sensitive

This commit is contained in:
Daniel Marjamäki 2008-11-26 18:13:36 +00:00
parent 2e22c7cb1d
commit 997f4e6165
2 changed files with 48 additions and 12 deletions

View File

@ -22,7 +22,8 @@
#include <stdlib.h> // free
#include <algorithm>
#include <iostream>
#include <sstream>
#ifdef __BORLANDC__
@ -219,14 +220,31 @@ const char * CheckMemoryLeakClass::call_func( const TOKEN *tok, std::list<const
while ( ftok && (ftok->str() != "{") )
ftok = ftok->next;
TOKEN *func = getcode( ftok->tokAt(1), callstack, parname, alloctype, dealloctype );
simplifycode( func );
simplifycode( func );
const TOKEN *func_ = func;
while ( func_ && func_->str() == ";" )
func_ = func_->next;
/*
for (const TOKEN *t = func_; t; t = t->next)
{
std::cout << t->str() << "\n";
}
*/
const char *ret = 0;
if (TOKEN::findmatch(func, "goto"))
ret = "dealloc"; // TODO : "goto" isn't handled well
else if (TOKEN::findmatch(func, "use"))
ret = "use";
else if (TOKEN::findmatch(func, "dealloc"))
if (TOKEN::findmatch(func_, "goto"))
{
// TODO : "goto" isn't handled well
if ( TOKEN::findmatch(func_, "dealloc") )
ret = "dealloc";
else if ( TOKEN::findmatch(func_, "use") )
ret = "use";
}
else if (TOKEN::Match(func_, "dealloc"))
ret = "dealloc";
else if (TOKEN::Match(func_, "use"))
ret = "use";
Tokenizer::deleteTokens(func);
return ret;
}
@ -613,6 +631,22 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
continue;
}
}
// Delete "if ; else ;"
if ( TOKEN::Match(tok2->next, "if ; else ;") )
{
erase( tok2, tok2->tokAt(4) );
done = false;
}
// TODO Make this more generic. Delete "if ; else use ; use"
if ( TOKEN::Match(tok2, "; if ; else use ; use") ||
TOKEN::Match(tok2, "; if use ; else ; use") )
{
erase( tok2, tok2->tokAt(4) );
done = false;
}
// Delete "if dealloc ;" and "if use ;" that is not followed by an else..
// This may cause false positives

View File

@ -23,7 +23,8 @@
#include "tokenize.h"
#include "CheckMemoryLeak.h"
#include "testsuite.h"
#include <iostream>
#include <sstream>
extern std::ostringstream errout;
@ -111,7 +112,7 @@ private:
TEST_CASE( func4 );
TEST_CASE( func5 );
TEST_CASE( func6 );
// TODO TEST_CASE( func7 );
TEST_CASE( func7 );
TEST_CASE( class1 );
TEST_CASE( class2 );
@ -779,7 +780,7 @@ private:
" foo(p);\n"
"}\n" );
std::string err( errout.str() );
ASSERT_EQUALS( std::string(""), err );
ASSERT_EQUALS( std::string("[test.cpp:10]: Memory leak: p\n"), err );
}
@ -797,8 +798,9 @@ private:
" char *p = new char[100];\n"
" foo(p);\n"
"}\n" );
std::string err( errout.str() );
ASSERT_EQUALS( std::string("[test.cpp:4]: Memory leak: p\n"), err );
std::string err( errout.str() );
std::cout << err << "\n";
ASSERT_EQUALS( std::string("[test.cpp:11]: Memory leak: p\n"), err );
}