CheckMemoryLeak: Fixed bug 2190219 - False positive, Mismatching allocation and deallocation

This commit is contained in:
Daniel Marjamäki 2008-10-25 16:47:51 +00:00
parent 30e1d5c06e
commit de71095e82
2 changed files with 57 additions and 1 deletions

View File

@ -198,6 +198,35 @@ static TOKEN *getcode(const TOKEN *tok, const char varname[])
varnames[0] = varname;
varnames[1] = 0;
// If "--all" hasn't been given and there are a preprocessor command, bail out.
// TODO: Evaluate all possible configurations separately instead.
if ( ! ShowAll )
{
int indentlevel = 0;
for ( const TOKEN *_tok = tok; _tok; _tok = _tok->next )
{
if ( _tok->str[0] == '{' )
{
indentlevel++;
}
else if ( _tok->str[0] == '}' )
{
if ( indentlevel <= 0 )
break;
indentlevel--;
}
else if ( _tok->str[0] == '#' )
{
return NULL;
}
}
}
TOKEN *rethead = 0, *rettail = 0;
#define addtoken(_str) \
{ \
@ -354,6 +383,7 @@ static TOKEN *getcode(const TOKEN *tok, const char varname[])
return rethead;
}
static void erase(TOKEN *begin, const TOKEN *end)
{
if ( ! begin )
@ -369,7 +399,11 @@ static void erase(TOKEN *begin, const TOKEN *end)
}
// Simpler but less powerful than "CheckMemoryLeak_CheckScope_All"
/**
* Check the allocation and deallocation for a function variable
* \param Tok1 The token where the variable is declared
* \param varname The name of the variable
**/
static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[] )
{
TOKEN *tok = getcode( Tok1, varname );

View File

@ -57,6 +57,7 @@ private:
TEST_CASE( switch2 );
TEST_CASE( mismatch1 );
TEST_CASE( mismatch2 );
TEST_CASE( func1 );
TEST_CASE( func2 );
@ -403,6 +404,27 @@ private:
}
void mismatch2()
{
// This code is taken directly from bug 2190219 - False positive, Mismatching allocation and deallocation
check( "void foo()\n"
"{\n"
"#ifdef __cplusplus\n"
" int* flags = new int[10];\n"
"#else\n"
" int* flags = (int*)malloc((10)*sizeof(int));\n"
"#endif\n"
"\n"
"#ifdef __cplusplus\n"
" delete [] flags;\n"
"#else\n"
" free(flags);\n"
"#endif\n"
"}\n");
ASSERT_EQUALS( std::string(""), errout.str() );
}