diff --git a/CheckMemoryLeak.cpp b/CheckMemoryLeak.cpp index 65f657583..d6aa8a62f 100644 --- a/CheckMemoryLeak.cpp +++ b/CheckMemoryLeak.cpp @@ -373,9 +373,32 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list { addtoken("if(!var)"); } - else if ( Match(tok, "if") || - Match(tok, "else") || - Match(tok, "switch") ) + else if ( Match(tok, "if") ) + { + // Check if the condition depends on var somehow.. + bool dep = false; + int parlevel = 0; + for ( const TOKEN *tok2 = tok; tok2; tok2 = tok2->next ) + { + if ( Match(tok2,"(") ) + ++parlevel; + if ( Match(tok2,")") ) + { + --parlevel; + if ( parlevel <= 0 ) + break; + } + if ( !Match(tok2,".") && + Match(tok2->next, "%var1%", varnames) && + !Match(tok2->next, "%var1% .", varnames) ) + { + dep = true; + break; + } + } + addtoken( (dep ? "ifv" : "if") ); + } + else if ( Match(tok, "else") || Match(tok, "switch") ) { addtoken(tok->str); } @@ -774,20 +797,35 @@ void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const MemoryLeak(findmatch(tok, "loop alloc ;"), varname); } - else if ( _settings._showAll && findmatch(tok, "alloc ; if continue ;") ) + else if ( findmatch(tok, "alloc ; if continue ;") ) { MemoryLeak(Tokenizer::gettok(findmatch(tok, "alloc ; if continue ;"), 3), varname); } - else if ( _settings._showAll && findmatch(tok, "alloc ; if break ;") ) + else if ( findmatch(tok, "alloc ; if break ;") ) { MemoryLeak(Tokenizer::gettok(findmatch(tok, "alloc ; if break ;"), 3), varname); } - else if ( _settings._showAll && findmatch(tok, "alloc ; if return ;") ) + else if ( findmatch(tok, "alloc ; if return ;") ) { MemoryLeak(Tokenizer::gettok(findmatch(tok, "alloc ; if return ;"), 3), varname); } + + else if ( _settings._showAll && findmatch(tok, "alloc ; ifv continue ;") ) + { + MemoryLeak(Tokenizer::gettok(findmatch(tok, "alloc ; ifv continue ;"), 3), varname); + } + + else if ( _settings._showAll && findmatch(tok, "alloc ; ifv break ;") ) + { + MemoryLeak(Tokenizer::gettok(findmatch(tok, "alloc ; ifv break ;"), 3), varname); + } + + else if ( _settings._showAll && findmatch(tok, "alloc ; ifv return ;") ) + { + MemoryLeak(Tokenizer::gettok(findmatch(tok, "alloc ; ifv return ;"), 3), varname); + } else if ( findmatch(tok, "alloc ; return ;") ) { diff --git a/testmemleak.cpp b/testmemleak.cpp index 60bed9c0f..e6ba5c8b0 100644 --- a/testmemleak.cpp +++ b/testmemleak.cpp @@ -74,7 +74,7 @@ private: TEST_CASE( use2 ); TEST_CASE( ifelse1 ); - // TODO TEST_CASE( ifelse2 ); + TEST_CASE( ifelse2 ); TEST_CASE( ifelse3 ); TEST_CASE( ifelse4 ); TEST_CASE( ifelse5 ); @@ -83,18 +83,19 @@ private: TEST_CASE( ifelse8 ); TEST_CASE( ifelse9 ); - // TODO TEST_CASE( if1 ); + TEST_CASE( if1 ); TEST_CASE( if2 ); TEST_CASE( if3 ); - TEST_CASE( if4 ); + TEST_CASE( if4 ); + TEST_CASE( if5 ); TEST_CASE( forwhile1 ); - // TODO TEST_CASE( forwhile2 ); + TEST_CASE( forwhile2 ); TEST_CASE( forwhile3 ); TEST_CASE( forwhile4 ); TEST_CASE( forwhile5 ); TEST_CASE( forwhile6 ); - // TODO TEST_CASE( forwhile7 ); + TEST_CASE( forwhile7 ); TEST_CASE( dowhile1 ); @@ -102,7 +103,7 @@ private: TEST_CASE( switch2 ); TEST_CASE( ret1 ); - // TODO TEST_CASE( ret2 ); + TEST_CASE( ret2 ); TEST_CASE( mismatch1 ); @@ -115,11 +116,11 @@ private: TEST_CASE( class1 ); TEST_CASE( class2 ); - // TODO TEST_CASE( throw1 ); + TEST_CASE( throw1 ); TEST_CASE( linux_list_1 ); - // TODO TEST_CASE( sizeof1 ); + TEST_CASE( sizeof1 ); } @@ -441,6 +442,19 @@ private: ASSERT_EQUALS( std::string(""), err ); } + void if5() + { + check( "void f()\n" + "{\n" + " char *p = malloc(256);\n" + " if (somecondition && !p)\n" + " return;\n" + " free(p);\n" + "}\n" ); + std::string err( errout.str() ); + ASSERT_EQUALS( std::string(""), err ); + } +