diff --git a/checkmemoryleak.cpp b/checkmemoryleak.cpp index 0a830c7d2..35224a322 100644 --- a/checkmemoryleak.cpp +++ b/checkmemoryleak.cpp @@ -576,7 +576,13 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list if ( TOKEN::Match(tok,"[)=] %var1% [+;)]", varnames) || TOKEN::Match(tok, "%var1% +=|-=", varnames) || TOKEN::Match(tok, "+=|<< %var1% ;", varnames) ) + { addtoken("use"); + } + else if ( TOKEN::Match(tok, "[;{}=(,+-*/] %var1% [", varnames) ) + { + addtoken("use_"); + } // Investigate function calls.. if ( TOKEN::Match(tok, "%var% (") ) @@ -1101,7 +1107,7 @@ void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const while ( TOKEN::Match(tok2, "[;{}] ;") ) erase(tok2, tok2->tokAt(2)); } - if ( (result = TOKEN::findmatch(tok, "dealloc [;{}] use ;")) != NULL ) + if ( (result = TOKEN::findmatch(tok, "dealloc [;{}] use|use_ ;")) != NULL ) { std::ostringstream errmsg; errmsg << _tokenizer->fileLine(result->tokAt(2)) << ": Using \"" << varname << "\" after it has been deallocated / released"; @@ -1113,7 +1119,7 @@ void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const { if (tok2->str() == "&use") tok2->str("use"); - else if (tok2->str() == "&use2") + else if (tok2->str() == "&use2" || tok2->str() == "use_") tok2->str(";"); else if (tok2->str() == "recursive" || tok2->str() == "dealloc_") tok2->str("dealloc"); diff --git a/testmemleak.cpp b/testmemleak.cpp index b38ea9938..be8ce903a 100644 --- a/testmemleak.cpp +++ b/testmemleak.cpp @@ -156,6 +156,7 @@ private: TEST_CASE( dealloc_use_2 ); // Deallocate and then use memory. No error if "use" is &var TEST_CASE( dealloc_use_3 ); // Deallocate and then use memory. No error TEST_CASE( dealloc_use_4 ); + TEST_CASE( dealloc_use_5 ); } @@ -1422,6 +1423,17 @@ private: ASSERT_EQUALS( std::string(""), errout.str() ); } + void dealloc_use_5() + { + check( "void foo()\n" + "{\n" + " char *str = 0;\n" + " free(str);\n" + " char c = str[10];\n" + "}\n" ); + ASSERT_EQUALS( std::string("[test.cpp:5]: Using \"str\" after it has been deallocated / released\n"), errout.str() ); + } + }; REGISTER_TEST( TestMemleak )