Memory Leak: Stronger checking. Ignore 'if use ;' and 'if dealloc;'. A leak could occur if it's not executed.

This commit is contained in:
Daniel Marjamäki 2008-11-09 08:40:57 +00:00
parent ef9f472188
commit c2ea705fd7
3 changed files with 27 additions and 2 deletions

View File

@ -518,6 +518,14 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[]
}
}
// Delete "if dealloc ;" and "if use ;" that is not followed by an else..
if ((Match(tok2, "[;{}] if dealloc ;") || Match(tok2, "[;{}] if use ;")) &&
!Match(Tokenizer::gettok(tok2,4), "else"))
{
erase(tok2->next, Tokenizer::gettok(tok2,3));
done = false;
}
// Delete if block: "alloc; if return use ;"
if (Match(tok2,"alloc ; if return use ;") && !Match(Tokenizer::gettok(tok2,6),"else"))
{

View File

@ -50,7 +50,7 @@ public:
_FuncName = FuncName;
}
const unsigned int file_id() const { return _FileId; }
unsigned int file_id() const { return _FileId; }
const std::string &name() const { return _FuncName; }
};

View File

@ -65,6 +65,8 @@ private:
TEST_CASE( simple7 );
TEST_CASE( simple8 );
TEST_CASE( use1 );
TEST_CASE( ifelse1 );
TEST_CASE( ifelse2 );
TEST_CASE( ifelse3 );
@ -192,6 +194,21 @@ private:
void use1()
{
check( "void foo()\n"
"{\n"
" char *str = strdup(\"abc\");\n"
" if (somecondition)\n"
" DeleteString(str);\n"
"}\n" );
ASSERT_EQUALS( std::string("[test.cpp:6]: Memory leak: str\n"), errout.str() );
}
void ifelse1()