2007-05-24 15:08:51 +02:00
|
|
|
|
|
|
|
#include "CheckMemoryLeak.h"
|
|
|
|
|
2007-07-17 08:39:34 +02:00
|
|
|
#include "tokenize.h"
|
2007-05-24 15:08:51 +02:00
|
|
|
|
|
|
|
#include "CommonCheck.h"
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <sstream>
|
|
|
|
|
2007-07-17 08:15:50 +02:00
|
|
|
#ifdef __linux__
|
|
|
|
#include <string.h>
|
|
|
|
#else
|
2007-05-24 15:08:51 +02:00
|
|
|
#include <mem.h> // <- memset
|
2007-07-17 08:15:50 +02:00
|
|
|
#endif
|
2007-05-24 15:08:51 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2007-07-20 17:43:39 +02:00
|
|
|
extern bool ShowAll;
|
2007-07-20 08:20:31 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-04-02 07:09:35 +02:00
|
|
|
static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char *varname[] )
|
2007-05-24 15:08:51 +02:00
|
|
|
{
|
2008-04-03 08:15:26 +02:00
|
|
|
// Check input pointers..
|
|
|
|
if ( ! (Tok1 && varname && varname[0] ) )
|
|
|
|
return;
|
|
|
|
|
|
|
|
int varc = 1;
|
|
|
|
while ( varname[varc] )
|
|
|
|
varc++;
|
|
|
|
varc = (varc - 1) * 2;
|
|
|
|
|
|
|
|
enum {No, Malloc, New, NewA} Alloc = No;
|
2007-05-24 15:08:51 +02:00
|
|
|
|
|
|
|
int indentlevel = 0;
|
2008-04-03 08:15:26 +02:00
|
|
|
for (const TOKEN *tok = Tok1 ; tok; tok = tok->next )
|
2007-05-24 15:08:51 +02:00
|
|
|
{
|
2008-04-02 07:09:35 +02:00
|
|
|
if (tok->str[0]=='{')
|
|
|
|
indentlevel++;
|
2007-05-24 15:08:51 +02:00
|
|
|
|
2008-04-02 07:09:35 +02:00
|
|
|
else if (tok->str[0]=='}')
|
|
|
|
{
|
|
|
|
indentlevel--;
|
2008-04-03 08:15:26 +02:00
|
|
|
if ( indentlevel < 0 && Alloc != No )
|
2008-04-02 07:09:35 +02:00
|
|
|
{
|
|
|
|
std::ostringstream errmsg;
|
|
|
|
errmsg << FileLine(Tok1) << ": Memory leak:" << varname[0];
|
|
|
|
ReportErr( errmsg.str() );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2007-05-24 15:08:51 +02:00
|
|
|
|
2008-04-03 08:15:26 +02:00
|
|
|
// Allocated..
|
|
|
|
if ( Match(tok, "%var1% =", varname) )
|
|
|
|
{
|
|
|
|
// What we may have...
|
|
|
|
// * var = (char *)malloc(10);
|
|
|
|
// * var = new char[10];
|
|
|
|
// * var = strdup("hello");
|
|
|
|
const TOKEN *tok2 = gettok(tok, varc+2);
|
|
|
|
if ( tok2 && tok2->str[0] == '(' )
|
|
|
|
{
|
|
|
|
while ( tok2 && tok2->str[0] != ')' )
|
|
|
|
tok2 = tok2->next;
|
|
|
|
tok2 = tok2 ? tok2->next : NULL;
|
|
|
|
}
|
|
|
|
if ( ! tok2 )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Does tok2 point on "malloc", "strdup" or "kmalloc"..
|
|
|
|
const char *mallocfunc[] = {"malloc", "strdup", "kmalloc", 0};
|
|
|
|
for ( unsigned int i = 0; mallocfunc[i]; i++ )
|
|
|
|
{
|
|
|
|
if ( strcmp(mallocfunc[i], tok2->str) == 0 )
|
|
|
|
{
|
|
|
|
Alloc = Malloc;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( Match( tok2, "new %type% [;(]" ) )
|
|
|
|
Alloc = New;
|
|
|
|
|
|
|
|
if ( Match( tok2, "new %type% [" ) )
|
|
|
|
Alloc = NewA;
|
|
|
|
}
|
|
|
|
|
2008-04-02 07:09:35 +02:00
|
|
|
// Deallocated..
|
|
|
|
if ( Match(tok, "delete %var1% ;", varname) )
|
|
|
|
return;
|
2007-05-24 15:08:51 +02:00
|
|
|
|
2008-04-02 07:09:35 +02:00
|
|
|
if ( Match(tok, "delete [ ] %var1% ;", varname) )
|
|
|
|
return;
|
2007-05-24 15:08:51 +02:00
|
|
|
|
2008-04-02 07:09:35 +02:00
|
|
|
if ( Match(tok, "free ( %var1% ) ;", varname) )
|
|
|
|
return;
|
2007-05-24 15:08:51 +02:00
|
|
|
|
2008-04-02 07:09:35 +02:00
|
|
|
if ( Match(tok, "kfree ( %var1% ) ;", varname) )
|
|
|
|
return;
|
2007-05-24 15:08:51 +02:00
|
|
|
|
2008-04-02 07:09:35 +02:00
|
|
|
// Used..
|
2008-04-02 11:37:05 +02:00
|
|
|
if ( Match( tok, "[=,(] %var1%", varname ) )
|
2008-04-02 07:09:35 +02:00
|
|
|
return;
|
|
|
|
if ( Match( tok, "return %var1%", varname ) )
|
|
|
|
return;
|
2007-05-24 15:08:51 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-06-10 20:25:39 +02:00
|
|
|
//---------------------------------------------------------------------------
|
2008-04-02 07:09:35 +02:00
|
|
|
// Checks for memory leaks inside function..
|
2007-06-10 20:25:39 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-04-02 07:09:35 +02:00
|
|
|
static void CheckMemoryLeak_InFunction()
|
2007-06-10 20:25:39 +02:00
|
|
|
{
|
2008-04-03 08:15:26 +02:00
|
|
|
bool infunc = false;
|
2008-04-02 07:09:35 +02:00
|
|
|
int indentlevel = 0;
|
|
|
|
for (const TOKEN *tok = tokens; tok; tok = tok->next)
|
2007-06-10 20:25:39 +02:00
|
|
|
{
|
2008-04-02 07:09:35 +02:00
|
|
|
if (tok->str[0]=='{')
|
|
|
|
indentlevel++;
|
2007-06-10 20:25:39 +02:00
|
|
|
|
2008-04-02 07:09:35 +02:00
|
|
|
else if (tok->str[0]=='}')
|
|
|
|
indentlevel--;
|
2007-06-10 20:25:39 +02:00
|
|
|
|
2008-04-03 08:15:26 +02:00
|
|
|
|
|
|
|
// In function..
|
|
|
|
if ( indentlevel == 0 )
|
2007-06-10 20:25:39 +02:00
|
|
|
{
|
2008-04-03 08:15:26 +02:00
|
|
|
if ( Match(tok, ") {") )
|
|
|
|
infunc = true;
|
|
|
|
|
|
|
|
if ( Match(tok, "[;}]") )
|
|
|
|
infunc = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Declare a local variable => Check
|
|
|
|
if (indentlevel>0 && infunc)
|
|
|
|
{
|
|
|
|
if ( Match(tok, "[{};] %type% * %var% [;=]") )
|
2007-06-10 20:25:39 +02:00
|
|
|
{
|
2008-04-03 08:15:26 +02:00
|
|
|
const char *varname[2] = {0,0};
|
|
|
|
varname[0] = getstr(tok, 3);
|
|
|
|
CheckMemoryLeak_CheckScope( tok->next, varname );
|
2007-06-10 20:25:39 +02:00
|
|
|
}
|
|
|
|
|
2008-04-03 08:15:26 +02:00
|
|
|
if ( Match(tok, "[{};] %type% %type% * %var% [;=]") )
|
2007-06-10 20:25:39 +02:00
|
|
|
{
|
2008-04-03 08:15:26 +02:00
|
|
|
const char *varname[2] = {0,0};
|
|
|
|
varname[0] = getstr(tok, 4);
|
|
|
|
CheckMemoryLeak_CheckScope( tok->next, varname );
|
2007-06-10 20:25:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-04-02 07:09:35 +02:00
|
|
|
//---------------------------------------------------------------------------
|
2007-06-10 20:25:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2008-04-02 07:09:35 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Checks for memory leaks in classes..
|
|
|
|
//---------------------------------------------------------------------------
|
2007-06-10 20:25:39 +02:00
|
|
|
|
2008-04-02 07:09:35 +02:00
|
|
|
static void CheckMemoryLeak_ClassMembers()
|
|
|
|
{
|
2007-06-10 20:25:39 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Checks for memory leaks..
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void CheckMemoryLeak()
|
|
|
|
{
|
|
|
|
// Check for memory leaks inside functions..
|
2008-04-02 07:09:35 +02:00
|
|
|
CheckMemoryLeak_InFunction();
|
2007-06-10 20:25:39 +02:00
|
|
|
|
|
|
|
// Check that all class members are deallocated..
|
2008-04-02 07:09:35 +02:00
|
|
|
CheckMemoryLeak_ClassMembers();
|
2007-06-10 20:25:39 +02:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-05-24 15:08:51 +02:00
|
|
|
|
2007-07-20 17:43:39 +02:00
|
|
|
|