From 271d8988aa4001de9780dae7c0bc9a03770706aa Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Tue, 7 Dec 2010 07:07:36 +0100 Subject: [PATCH] Symbol database: fixed a TODO test where class variables were qualified with the class name --- lib/checkmemoryleak.cpp | 62 ++++++++++++++++++++--------------------- lib/checkmemoryleak.h | 4 +-- test/testmemleak.cpp | 3 +- 3 files changed, 34 insertions(+), 35 deletions(-) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 92ba3ad49..2f8ae1578 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -238,53 +238,43 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getDeallocationType(const Token *tok return No; } -CheckMemoryLeak::AllocType CheckMemoryLeak::getDeallocationType(const Token *tok, const char *varnames[]) const +CheckMemoryLeak::AllocType CheckMemoryLeak::getDeallocationType(const Token *tok, const std::string &varname) const { - int i = 0; - std::string names; - while (varnames[i]) - { - if (i > 0) - names += " . "; - - names += varnames[i]; - i++; - } - - if (Token::simpleMatch(tok, std::string("delete " + names + " ;").c_str())) + if (Token::simpleMatch(tok, std::string("delete " + varname + " ;").c_str())) return New; - if (Token::simpleMatch(tok, std::string("delete [ ] " + names + " ;").c_str())) + if (Token::simpleMatch(tok, std::string("delete [ ] " + varname + " ;").c_str())) return NewArray; - if (Token::simpleMatch(tok, std::string("delete ( " + names + " ) ;").c_str())) + if (Token::simpleMatch(tok, std::string("delete ( " + varname + " ) ;").c_str())) return New; - if (Token::simpleMatch(tok, std::string("delete [ ] ( " + names + " ) ;").c_str())) + if (Token::simpleMatch(tok, std::string("delete [ ] ( " + varname + " ) ;").c_str())) return NewArray; - if (Token::simpleMatch(tok, std::string("free ( " + names + " ) ;").c_str()) || - Token::simpleMatch(tok, std::string("kfree ( " + names + " ) ;").c_str())) + if (Token::simpleMatch(tok, std::string("free ( " + varname + " ) ;").c_str()) || + Token::simpleMatch(tok, std::string("kfree ( " + varname + " ) ;").c_str())) return Malloc; - if (Token::simpleMatch(tok, std::string("g_free ( " + names + " ) ;").c_str())) + if (Token::simpleMatch(tok, std::string("g_free ( " + varname + " ) ;").c_str())) return gMalloc; - if (Token::simpleMatch(tok, std::string("fclose ( " + names + " )").c_str()) || + if (Token::simpleMatch(tok, std::string("fclose ( " + varname + " )").c_str()) || Token::simpleMatch(tok, "fcloseall ( )")) return File; - if (Token::simpleMatch(tok, std::string("close ( " + names + " )").c_str())) + if (Token::simpleMatch(tok, std::string("close ( " + varname + " )").c_str())) return Fd; - if (Token::simpleMatch(tok, std::string("pclose ( " + names + " )").c_str())) + if (Token::simpleMatch(tok, std::string("pclose ( " + varname + " )").c_str())) return Pipe; - if (Token::simpleMatch(tok, std::string("closedir ( " + names + " )").c_str())) + if (Token::simpleMatch(tok, std::string("closedir ( " + varname + " )").c_str())) return Dir; return No; } + //-------------------------------------------------------------------------- @@ -2689,8 +2679,9 @@ void CheckMemoryLeakInClass::variable(const SymbolDatabase::SpaceInfo *classinfo return; // Foo::var1 = .. - // bail out - if (Token::simpleMatch(tok->previous(), "::")) + // bail out when not same class + if (Token::simpleMatch(tok->previous(), "::") && + tok->strAt(-2) != classinfo->className) return; AllocType alloc = getAllocationType(tok->tokAt((indent > 0) ? 2 : 3), 0); @@ -2718,14 +2709,16 @@ void CheckMemoryLeakInClass::variable(const SymbolDatabase::SpaceInfo *classinfo continue; // Deallocate.. - const char *varnames[3] = { "var", 0, 0 }; - varnames[0] = varname.c_str(); - AllocType dealloc = getDeallocationType(tok, varnames); + AllocType dealloc = getDeallocationType(tok, varname); if (dealloc == No) { - varnames[0] = "this"; - varnames[1] = varname.c_str(); - dealloc = getDeallocationType(tok, varnames); + std::string temp = classinfo->className + " :: " + varname; + dealloc = getDeallocationType(tok, temp); + } + if (dealloc == No) + { + std::string temp = "this . " + varname; + dealloc = getDeallocationType(tok, temp); } if (dealloc != CheckMemoryLeak::No) { @@ -2800,6 +2793,13 @@ void CheckMemoryLeakInClass::checkPublicFunctions(const SymbolDatabase::SpaceInf if (alloc != CheckMemoryLeak::No) publicAllocationError(tok2, tok2->strAt(1)); } + else if (Token::Match(tok2, "{|}|; %type% :: %varid% =", varid) && + tok2->next()->str() == spaceinfo->className) + { + const CheckMemoryLeak::AllocType alloc = getAllocationType(tok2->tokAt(5), varid); + if (alloc != CheckMemoryLeak::No) + publicAllocationError(tok2, tok2->strAt(3)); + } } } } diff --git a/lib/checkmemoryleak.h b/lib/checkmemoryleak.h index 828008df4..aea47bac5 100644 --- a/lib/checkmemoryleak.h +++ b/lib/checkmemoryleak.h @@ -98,10 +98,10 @@ public: /** * @brief Get type of deallocation at given position * @param tok position - * @param varnames variable names + * @param varname variable name * @return type of deallocation */ - AllocType getDeallocationType(const Token *tok, const char *varnames[]) const; + AllocType getDeallocationType(const Token *tok, const std::string &varname) const; /** * @brief Get type of deallocation at given position diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 95d173d9c..942e529cc 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -3685,8 +3685,7 @@ private: " A::pd = new char[12];\n" " delete [] A::pd;\n" "}\n"); - ASSERT_EQUALS("", errout.str()); - TODO_ASSERT_EQUALS("[test.cpp:9]: (warning) Possible leak in public function. The pointer 'pd' is not deallocated before it is allocated.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:9]: (warning) Possible leak in public function. The pointer 'pd' is not deallocated before it is allocated.\n", errout.str()); check("class A {\n" "private:\n"