From 4957e48d7c29f8e88f13d34de8766968ba4b171f Mon Sep 17 00:00:00 2001 From: PKEuS Date: Wed, 18 Nov 2015 21:17:50 +0100 Subject: [PATCH] Refactorization: Removed std::string overload of CheckMemoryLeak::getDeallocationType(), improved varid overload to handle member variables --- lib/checkmemoryleak.cpp | 113 ++++++++++++---------------------------- lib/checkmemoryleak.h | 8 --- 2 files changed, 33 insertions(+), 88 deletions(-) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 460549bbb..8b30a850d 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -226,94 +226,55 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getReallocationType(const Token *tok if (tok2->str() == "realloc") return Malloc; - // GTK memory reallocation.. - //if (Token::Match(tok2, "g_realloc|g_try_realloc|g_renew|g_try_renew")) - return No; } CheckMemoryLeak::AllocType CheckMemoryLeak::getDeallocationType(const Token *tok, unsigned int varid) const { - if (tokenizer->isCPP()) { - if (Token::Match(tok, "delete %varid% ;", varid)) + if (tokenizer->isCPP() && tok->str() == "delete") { + const Token* vartok = tok->astOperand1() ? tok->astOperand1() : tok->next(); + if (Token::Match(vartok, ".|::")) + vartok = vartok->astOperand2(); + + if (vartok && vartok->varId() == varid) { + if (tok->strAt(1) == "[") + return NewArray; return New; - - if (Token::Match(tok, "delete [ ] %varid% ;", varid)) - return NewArray; - - if (Token::Match(tok, "delete ( %varid% ) ;", varid)) - return New; - - if (Token::Match(tok, "delete [ ] ( %varid% ) ;", varid)) - return NewArray; + } } if (tok && tok->str() == "::") tok = tok->next(); - if (Token::Match(tok, "free|kfree ( %varid% ) [;:]", varid) || - Token::Match(tok, "free|kfree ( %varid% -|,", varid) || - Token::Match(tok, "realloc ( %varid% , 0 ) ;", varid)) - return Malloc; + if (Token::Match(tok, "%name% (")) { + if (Token::simpleMatch(tok, "fcloseall ( )")) + return File; - if (Token::Match(tok, "fclose ( %varid% )", varid) || - Token::simpleMatch(tok, "fcloseall ( )")) - return File; + const Token* vartok = tok->tokAt(2); + while (Token::Match(vartok, "%name% .|::")) + vartok = vartok->tokAt(2); - if (settings1->standards.posix) { - if (Token::Match(tok, "close ( %varid% )", varid)) - return Fd; + if (Token::Match(vartok, "%varid% )|,|-", varid)) { + if (Token::Match(tok, "free|kfree") || + (tok->str() == "realloc" && Token::Match(vartok->next(), ", 0 )"))) + return Malloc; - if (Token::Match(tok, "pclose ( %varid% )", varid)) - return Pipe; - } + if (tok->str() == "fclose") + return File; - // Does tok2 point on "g_free", etc .. - if (Token::Match(tok, "%type% ( %varid% )", varid)) { - const int dealloctype = settings1->library.dealloc(tok); - if (dealloctype > 0) - return Library::ismemory(dealloctype) ? OtherMem : OtherRes; - } + if (settings1->standards.posix) { + if (tok->str() == "close") + return Fd; + if (tok->str() == "pclose") + return Pipe; + } - return No; -} - -CheckMemoryLeak::AllocType CheckMemoryLeak::getDeallocationType(const Token *tok, const std::string &varname) const -{ - if (tokenizer->isCPP()) { - if (Token::Match(tok, std::string("delete " + varname + " [,;]").c_str())) - return New; - - if (Token::Match(tok, std::string("delete [ ] " + varname + " [,;]").c_str())) - return NewArray; - - if (Token::Match(tok, std::string("delete ( " + varname + " ) [,;]").c_str())) - return New; - - if (Token::Match(tok, std::string("delete [ ] ( " + varname + " ) [,;]").c_str())) - return NewArray; - } - - if (Token::simpleMatch(tok, std::string("free ( " + varname + " ) ;").c_str()) || - Token::simpleMatch(tok, std::string("kfree ( " + varname + " ) ;").c_str()) || - Token::simpleMatch(tok, std::string("realloc ( " + varname + " , 0 ) ;").c_str())) - return Malloc; - - if (Token::simpleMatch(tok, std::string("fclose ( " + varname + " )").c_str()) || - Token::simpleMatch(tok, "fcloseall ( )")) - return File; - - if (Token::simpleMatch(tok, std::string("close ( " + varname + " )").c_str())) - return Fd; - - if (Token::simpleMatch(tok, std::string("pclose ( " + varname + " )").c_str())) - return Pipe; - - if (Token::Match(tok, ("%type% ( " + varname + " )").c_str())) { - int type = settings1->library.dealloc(tok); - if (type > 0) - return Library::ismemory(type) ? OtherMem : OtherRes; + // Does tok2 point on "g_free", etc .. + const int dealloctype = settings1->library.dealloc(tok); + if (dealloctype > 0) + return Library::ismemory(dealloctype) ? OtherMem : OtherRes; + } } return No; @@ -2384,15 +2345,7 @@ void CheckMemoryLeakInClass::variable(const Scope *scope, const Token *tokVarnam continue; // Deallocate.. - AllocType dealloc = getDeallocationType(tok, varname); - if (dealloc == No) { - std::string temp = scope->className + " :: " + varname; - dealloc = getDeallocationType(tok, temp); - } - if (dealloc == No) { - std::string temp = "this . " + varname; - dealloc = getDeallocationType(tok, temp); - } + AllocType dealloc = getDeallocationType(tok, varid); // some usage in the destructor => assume it's related // to deallocation if (destructor && tok->str() == varname) diff --git a/lib/checkmemoryleak.h b/lib/checkmemoryleak.h index fb7d54738..c7540a866 100644 --- a/lib/checkmemoryleak.h +++ b/lib/checkmemoryleak.h @@ -97,14 +97,6 @@ public: void memoryLeak(const Token *tok, const std::string &varname, AllocType alloctype); - /** - * @brief Get type of deallocation at given position - * @param tok position - * @param varname variable name - * @return type of deallocation - */ - AllocType getDeallocationType(const Token *tok, const std::string &varname) const; - /** * @brief Get type of deallocation at given position * @param tok position