cppcheck/lib/checkmemoryleak.cpp

2787 lines
107 KiB
C++
Raw Normal View History

/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2018 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "checkmemoryleak.h"
2017-05-27 04:33:47 +02:00
#include "astutils.h"
#include "library.h"
#include "mathlib.h"
2017-05-27 04:33:47 +02:00
#include "settings.h"
#include "standards.h"
#include "symboldatabase.h"
#include "token.h"
2009-03-20 18:16:21 +01:00
#include "tokenize.h"
2017-05-27 04:33:47 +02:00
#include "tokenlist.h"
#include "utils.h"
2017-05-27 04:33:47 +02:00
#include "valueflow.h"
2009-03-20 18:16:21 +01:00
#include <algorithm>
2017-05-27 04:33:47 +02:00
#include <cstddef>
#include <set>
#include <stack>
//---------------------------------------------------------------------------
2009-03-20 18:16:21 +01:00
// Register this check class (by creating a static instance of it)
2011-10-13 20:53:06 +02:00
namespace {
CheckMemoryLeakInFunction instance1;
2011-10-13 20:53:06 +02:00
CheckMemoryLeakInClass instance2;
CheckMemoryLeakStructMember instance3;
CheckMemoryLeakNoVar instance4;
}
// CWE ID used:
static const CWE CWE398(398U); // Indicator of Poor Code Quality
static const CWE CWE401(401U); // Improper Release of Memory Before Removing Last Reference ('Memory Leak')
static const CWE CWE771(771U); // Missing Reference to Active Allocated Resource
static const CWE CWE772(772U); // Missing Release of Resource after Effective Lifetime
/**
* Count function parameters
* \param tok Function name token before the '('
*/
static unsigned int countParameters(const Token *tok)
{
tok = tok->tokAt(2);
if (tok->str() == ")")
return 0;
unsigned int numpar = 1;
while (nullptr != (tok = tok->nextArgument()))
numpar++;
return numpar;
}
/** List of functions that can be ignored when searching for memory leaks.
* These functions don't take the address of the given pointer
* This list contains function names with const parameters e.g.: atof(const char *)
* TODO: This list should be replaced by <leak-ignore/> in .cfg files.
*/
static const std::set<std::string> call_func_white_list = {
"_open", "_wopen", "access", "adjtime", "asctime_r", "asprintf", "chdir", "chmod", "chown"
, "creat", "ctime_r", "execl", "execle", "execlp", "execv", "execve", "fchmod", "fcntl"
, "fdatasync", "fclose", "flock", "fmemopen", "fnmatch", "fopen", "fopencookie", "for", "free"
, "freopen", "fseeko", "fstat", "fsync", "ftello", "ftruncate", "getgrnam", "gethostbyaddr", "gethostbyname"
, "getnetbyname", "getopt", "getopt_long", "getprotobyname", "getpwnam", "getservbyname", "getservbyport"
, "glob", "gmtime", "gmtime_r", "if", "index", "inet_addr", "inet_aton", "inet_network", "initgroups"
, "ioctl", "link", "localtime_r", "lockf", "lseek", "lstat", "mkdir", "mkfifo", "mknod", "mkstemp"
, "obstack_printf", "obstack_vprintf", "open", "opendir", "parse_printf_format", "pathconf"
, "perror", "popen", "posix_fadvise", "posix_fallocate", "pread", "psignal", "pwrite", "read", "readahead"
, "readdir", "readdir_r", "readlink", "readv", "realloc", "regcomp", "return", "rewinddir", "rindex"
, "rmdir", "scandir", "seekdir", "setbuffer", "sethostname", "setlinebuf", "sizeof", "strdup"
, "stat", "stpcpy", "strcasecmp", "stricmp", "strncasecmp", "switch"
, "symlink", "sync_file_range", "telldir", "tempnam", "time", "typeid", "unlink"
, "utime", "utimes", "vasprintf", "while", "wordexp", "write", "writev"
};
2009-03-20 18:16:21 +01:00
//---------------------------------------------------------------------------
bool CheckMemoryLeak::isclass(const Token *tok, unsigned int varid) const
{
if (tok->isStandardType())
return false;
const Variable * var = tokenizer->getSymbolDatabase()->getVariableFromVarId(varid);
// return false if the type is a simple record type without side effects
// a type that has no side effects (no constructors and no members with constructors)
/** @todo false negative: check base class for side effects */
/** @todo false negative: check constructors for side effects */
if (var && var->typeScope() && var->typeScope()->numConstructors == 0 &&
(var->typeScope()->varlist.empty() || var->type()->needInitialization == Type::True) &&
var->type()->derivedFrom.empty())
return false;
return true;
}
//---------------------------------------------------------------------------
CheckMemoryLeak::AllocType CheckMemoryLeak::getAllocationType(const Token *tok2, unsigned int varid, std::list<const Function*> *callstack) const
{
// What we may have...
// * var = (char *)malloc(10);
// * var = new char[10];
// * var = strdup("hello");
// * var = strndup("hello", 3);
2011-10-13 20:53:06 +02:00
if (tok2 && tok2->str() == "(") {
tok2 = tok2->link();
2014-02-15 16:17:25 +01:00
tok2 = tok2 ? tok2->next() : nullptr;
}
if (! tok2)
return No;
if (tok2->str() == "::")
tok2 = tok2->next();
if (! tok2->isName())
return No;
if (!Token::Match(tok2, "%name% ::|. %type%")) {
// Using realloc..
if (varid && Token::Match(tok2, "realloc ( %any% ,") && tok2->tokAt(2)->varId() != varid)
return Malloc;
if (tokenizer->isCPP() && tok2->str() == "new") {
if (tok2->strAt(1) == "(" && !Token::Match(tok2->next(),"( std| ::| nothrow )"))
return No;
if (tok2->astOperand1() && (tok2->astOperand1()->str() == "[" || (tok2->astOperand1()->astOperand1() && tok2->astOperand1()->astOperand1()->str() == "[")))
return NewArray;
return New;
}
2013-07-05 20:55:31 +02:00
if (settings1->standards.posix) {
if (Token::Match(tok2, "open|openat|creat|mkstemp|mkostemp|socket (")) {
2013-06-29 12:55:24 +02:00
// simple sanity check of function parameters..
// TODO: Make such check for all these functions
2018-04-04 21:51:31 +02:00
const unsigned int num = countParameters(tok2);
2013-06-29 12:55:24 +02:00
if (tok2->str() == "open" && num != 2 && num != 3)
return No;
// is there a user function with this name?
if (tok2->function())
2013-06-29 12:55:24 +02:00
return No;
return Fd;
}
if (Token::simpleMatch(tok2, "popen ("))
return Pipe;
}
// Does tok2 point on a Library allocation function?
const int alloctype = settings1->library.alloc(tok2, -1);
if (alloctype > 0) {
if (alloctype == settings1->library.deallocId("free"))
return Malloc;
if (alloctype == settings1->library.deallocId("fclose"))
return File;
return Library::ismemory(alloctype) ? OtherMem : OtherRes;
}
}
while (Token::Match(tok2,"%name% ::|. %type%"))
tok2 = tok2->tokAt(2);
// User function
const Function* func = tok2->function();
2014-02-15 16:17:25 +01:00
if (func == nullptr)
return No;
// Prevent recursion
if (callstack && std::find(callstack->begin(), callstack->end(), func) != callstack->end())
return No;
std::list<const Function*> cs;
if (!callstack)
callstack = &cs;
callstack->push_back(func);
return functionReturnType(func, callstack);
}
CheckMemoryLeak::AllocType CheckMemoryLeak::getReallocationType(const Token *tok2, unsigned int varid)
{
// What we may have...
// * var = (char *)realloc(..;
2011-10-13 20:53:06 +02:00
if (tok2 && tok2->str() == "(") {
tok2 = tok2->link();
2014-02-15 16:17:25 +01:00
tok2 = tok2 ? tok2->next() : nullptr;
}
if (! tok2)
return No;
if (varid > 0 && ! Token::Match(tok2, "%name% ( %varid% [,)]", varid))
return No;
if (tok2->str() == "realloc")
return Malloc;
return No;
}
CheckMemoryLeak::AllocType CheckMemoryLeak::getDeallocationType(const Token *tok, unsigned int varid) const
{
if (tokenizer->isCPP() && tok->str() == "delete" && tok->astOperand1()) {
const Token* vartok = tok->astOperand1();
if (Token::Match(vartok, ".|::"))
vartok = vartok->astOperand2();
if (vartok && vartok->varId() == varid) {
if (tok->strAt(1) == "[")
return NewArray;
return New;
}
}
2016-01-16 14:15:51 +01:00
if (tok->str() == "::")
tok = tok->next();
if (Token::Match(tok, "%name% (")) {
if (Token::simpleMatch(tok, "fcloseall ( )"))
return File;
int argNr = 1;
for (const Token* tok2 = tok->tokAt(2); tok2; tok2 = tok2->nextArgument()) {
const Token* vartok = tok2;
while (Token::Match(vartok, "%name% .|::"))
vartok = vartok->tokAt(2);
if (Token::Match(vartok, "%varid% )|,|-", varid)) {
if (tok->str() == "realloc" && Token::simpleMatch(vartok->next(), ", 0 )"))
return Malloc;
if (settings1->standards.posix) {
if (tok->str() == "close")
return Fd;
if (tok->str() == "pclose")
return Pipe;
}
// Does tok point on a Library deallocation function?
const int dealloctype = settings1->library.dealloc(tok, argNr);
if (dealloctype > 0) {
if (dealloctype == settings1->library.deallocId("free"))
return Malloc;
if (dealloctype == settings1->library.deallocId("fclose"))
return File;
return Library::ismemory(dealloctype) ? OtherMem : OtherRes;
}
}
argNr++;
}
2013-07-05 20:55:31 +02:00
}
return No;
}
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
void CheckMemoryLeak::memoryLeak(const Token *tok, const std::string &varname, AllocType alloctype) const
{
if (alloctype == CheckMemoryLeak::File ||
alloctype == CheckMemoryLeak::Pipe ||
alloctype == CheckMemoryLeak::Fd ||
alloctype == CheckMemoryLeak::OtherRes)
2011-12-08 21:28:34 +01:00
resourceLeakError(tok, varname);
else
2011-12-08 21:28:34 +01:00
memleakError(tok, varname);
}
//---------------------------------------------------------------------------
2016-02-27 16:03:50 +01:00
void CheckMemoryLeak::reportErr(const Token *tok, Severity::SeverityType severity, const std::string &id, const std::string &msg, const CWE &cwe) const
{
2009-08-04 21:36:55 +02:00
std::list<const Token *> callstack;
if (tok)
2009-08-04 21:36:55 +02:00
callstack.push_back(tok);
reportErr(callstack, severity, id, msg, cwe);
}
2016-02-27 16:03:50 +01:00
void CheckMemoryLeak::reportErr(const std::list<const Token *> &callstack, Severity::SeverityType severity, const std::string &id, const std::string &msg, const CWE &cwe) const
{
const ErrorLogger::ErrorMessage errmsg(callstack, tokenizer ? &tokenizer->list : nullptr, severity, id, msg, cwe, false);
if (errorLogger)
2009-08-04 21:36:55 +02:00
errorLogger->reportErr(errmsg);
else
Check::reportError(errmsg);
}
void CheckMemoryLeak::memleakError(const Token *tok, const std::string &varname) const
{
reportErr(tok, Severity::error, "memleak", "$symbol:" + varname + "\nMemory leak: $symbol", CWE(401U));
}
void CheckMemoryLeak::memleakUponReallocFailureError(const Token *tok, const std::string &varname) const
{
reportErr(tok, Severity::error, "memleakOnRealloc", "$symbol:" + varname + "\nCommon realloc mistake: \'$symbol\' nulled but not freed upon failure", CWE(401U));
}
void CheckMemoryLeak::resourceLeakError(const Token *tok, const std::string &varname) const
{
std::string errmsg("Resource leak");
if (!varname.empty())
errmsg = "$symbol:" + varname + '\n' + errmsg + ": $symbol";
2016-02-27 16:03:50 +01:00
reportErr(tok, Severity::error, "resourceLeak", errmsg, CWE(775U));
}
void CheckMemoryLeak::deallocDeallocError(const Token *tok, const std::string &varname) const
{
reportErr(tok, Severity::error, "deallocDealloc", "$symbol:" + varname + "\nDeallocating a deallocated pointer: $symbol", CWE(415U));
}
void CheckMemoryLeak::deallocuseError(const Token *tok, const std::string &varname) const
{
reportErr(tok, Severity::error, "deallocuse", "$symbol:" + varname + "\nDereferencing '$symbol' after it is deallocated / released", CWE(416U));
}
void CheckMemoryLeak::mismatchSizeError(const Token *tok, const std::string &sz) const
{
2016-02-27 16:03:50 +01:00
reportErr(tok, Severity::error, "mismatchSize", "The allocated size " + sz + " is not a multiple of the underlying type's size.", CWE(131U));
}
void CheckMemoryLeak::mismatchAllocDealloc(const std::list<const Token *> &callstack, const std::string &varname) const
{
reportErr(callstack, Severity::error, "mismatchAllocDealloc", "$symbol:" + varname + "\nMismatching allocation and deallocation: $symbol", CWE(762U));
}
CheckMemoryLeak::AllocType CheckMemoryLeak::functionReturnType(const Function* func, std::list<const Function*> *callstack) const
{
if (!func || !func->hasBody())
return No;
// Get return pointer..
unsigned int varid = 0;
for (const Token *tok2 = func->functionScope->bodyStart; tok2 != func->functionScope->bodyEnd; tok2 = tok2->next()) {
if (tok2->str() == "return") {
2018-04-04 21:51:31 +02:00
const AllocType allocType = getAllocationType(tok2->next(), 0, callstack);
if (allocType != No)
return allocType;
if (tok2->scope() != func->functionScope || !tok2->astOperand1())
return No;
const Token* tok = tok2->astOperand1();
if (Token::Match(tok, ".|::"))
tok = tok->astOperand2() ? tok->astOperand2() : tok->astOperand1();
if (tok)
varid = tok->varId();
break;
}
}
// Not returning pointer value..
if (varid == 0)
return No;
2014-06-12 19:58:43 +02:00
// If variable is not local then alloctype shall be "No"
// Todo: there can be false negatives about mismatching allocation/deallocation.
// => Generate "alloc ; use ;" if variable is not local?
const Variable *var = tokenizer->getSymbolDatabase()->getVariableFromVarId(varid);
if (!var || !var->isLocal() || var->isStatic())
return No;
// Check if return pointer is allocated..
AllocType allocType = No;
for (const Token* tok = func->functionScope->bodyStart; tok != func->functionScope->bodyEnd; tok = tok->next()) {
2011-10-13 20:53:06 +02:00
if (Token::Match(tok, "%varid% =", varid)) {
allocType = getAllocationType(tok->tokAt(2), varid, callstack);
}
2011-10-13 20:53:06 +02:00
if (Token::Match(tok, "= %varid% ;", varid)) {
return No;
}
if (!tokenizer->isC() && Token::Match(tok, "[(,] %varid% [,)]", varid)) {
return No;
}
if (Token::Match(tok, "[(,] & %varid% [.,)]", varid)) {
return No;
}
if (Token::Match(tok, "[;{}] %varid% .", varid)) {
return No;
}
if (allocType == No && tok->str() == "return")
return No;
}
return allocType;
}
const char *CheckMemoryLeak::functionArgAlloc(const Function *func, unsigned int targetpar, AllocType &allocType) const
{
allocType = No;
if (!func || !func->functionScope)
return "";
if (!Token::simpleMatch(func->retDef, "void"))
return "";
std::list<Variable>::const_iterator arg = func->argumentList.begin();
for (; arg != func->argumentList.end(); ++arg) {
if (arg->index() == targetpar-1)
break;
}
if (arg == func->argumentList.end())
return "";
// Is **
if (!arg->isPointer())
return "";
const Token* tok = arg->typeEndToken();
tok = tok->previous();
if (tok->str() != "*")
return "";
// Check if pointer is allocated.
2017-10-08 07:54:39 +02:00
bool realloc = false;
for (tok = func->functionScope->bodyStart; tok && tok != func->functionScope->bodyEnd; tok = tok->next()) {
if (tok->varId() == arg->declarationId()) {
if (Token::Match(tok->tokAt(-3), "free ( * %name% )")) {
2017-10-08 07:54:39 +02:00
realloc = true;
allocType = No;
} else if (Token::Match(tok->previous(), "* %name% =")) {
allocType = getAllocationType(tok->tokAt(2), arg->declarationId());
2011-10-13 20:53:06 +02:00
if (allocType == No) {
allocType = getReallocationType(tok->tokAt(2), arg->declarationId());
}
2011-10-13 20:53:06 +02:00
if (allocType != No) {
if (realloc)
return "realloc";
return "alloc";
}
2011-10-13 20:53:06 +02:00
} else {
// unhandled variable usage: bailout
return "";
}
}
}
return "";
}
static bool notvar(const Token *tok, unsigned int varid)
{
2015-07-25 11:37:03 +02:00
if (!tok)
return false;
if (Token::Match(tok, "&&|;"))
return notvar(tok->astOperand1(),varid) || notvar(tok->astOperand2(),varid);
2015-07-25 11:37:03 +02:00
if (tok->str() == "(" && Token::Match(tok->astOperand1(), "UNLIKELY|LIKELY"))
return notvar(tok->astOperand2(), varid);
2015-08-03 09:20:50 +02:00
const Token *vartok = astIsVariableComparison(tok, "==", "0");
2015-07-22 12:31:18 +02:00
return vartok && (vartok->varId() == varid);
}
static bool ifvar(const Token *tok, unsigned int varid, const std::string &comp, const std::string &rhs)
{
if (!Token::simpleMatch(tok, "if ("))
return false;
2015-07-25 11:37:03 +02:00
const Token *condition = tok->next()->astOperand2();
if (condition && condition->str() == "(" && Token::Match(condition->astOperand1(), "UNLIKELY|LIKELY"))
condition = condition->astOperand2();
if (!condition || condition->str() == "&&")
return false;
2015-07-25 11:37:03 +02:00
2015-08-03 09:20:50 +02:00
const Token *vartok = astIsVariableComparison(condition, comp, rhs);
return (vartok && vartok->varId() == varid);
}
static bool alwaysTrue(const Token *tok)
{
if (!tok)
return false;
if (tok->values().size() == 1U &&
2017-12-20 11:16:59 +01:00
tok->values().front().isKnown() &&
tok->values().front().intvalue != 0)
return true;
if (tok->str() == "||")
return alwaysTrue(tok->astOperand1()) || alwaysTrue(tok->astOperand2());
if (tok->str() == "true")
return true;
2017-12-20 11:16:59 +01:00
return false;
}
bool CheckMemoryLeakInFunction::test_white_list(const std::string &funcname, const Settings *settings, bool cpp)
{
2016-12-06 12:31:16 +01:00
return ((call_func_white_list.find(funcname)!=call_func_white_list.end()) || settings->library.isLeakIgnore(funcname) || (cpp && funcname == "delete"));
}
namespace {
const std::set<std::string> call_func_keywords = {
"asprintf"
, "delete"
, "fclose"
, "for"
, "free"
, "if"
, "realloc"
, "return"
, "switch"
, "while"
, "sizeof"
};
}
const char * CheckMemoryLeakInFunction::call_func(const Token *tok, std::list<const Token *> callstack, const unsigned int varid, AllocType &alloctype, AllocType &dealloctype, bool &allocpar, unsigned int sz)
{
if (test_white_list(tok->str(), mSettings, tokenizer->isCPP())) {
if (call_func_keywords.find(tok->str())!=call_func_keywords.end())
return nullptr;
// is the varid a parameter?
for (const Token *tok2 = tok->tokAt(2); tok2 && tok2 != tok->linkAt(1); tok2 = tok2->next()) {
if (tok2->str() == "(") {
tok2 = tok2->nextArgument();
if (!tok2)
break;
}
2011-10-13 20:53:06 +02:00
if (tok2->varId() == varid) {
if (tok->strAt(-1) == ".")
return "use";
else if (tok2->strAt(1) == "=")
return "assign";
else if (tok->str()=="printf")
return "use"; // <- it is not certain printf dereference the pointer TODO: check the format string
else
return "use_";
}
}
return nullptr;
}
if (mSettings->library.isnoreturn(tok) && tok->strAt(-1) != "=")
return "exit";
if (varid > 0 && (getReallocationType(tok, varid) != No || getDeallocationType(tok, varid) != No))
return nullptr;
if (callstack.size() > 2)
return "dealloc_";
const std::string& funcname(tok->str());
2011-10-13 20:53:06 +02:00
for (std::list<const Token *>::const_iterator it = callstack.begin(); it != callstack.end(); ++it) {
if ((*it) && (*it)->str() == funcname)
return "recursive";
}
callstack.push_back(tok);
// lock/unlock..
2011-10-13 20:53:06 +02:00
if (varid == 0) {
const Function* func = tok->function();
if (!func || !func->hasBody())
return nullptr;
Token *ftok = getcode(func->functionScope->bodyStart->next(), callstack, 0, alloctype, dealloctype, false, 1);
simplifycode(ftok);
const char *ret = nullptr;
if (Token::simpleMatch(ftok, "; alloc ; }"))
ret = "alloc";
else if (Token::simpleMatch(ftok, "; dealloc ; }"))
ret = "dealloc";
TokenList::deleteTokens(ftok);
return ret;
}
// how many parameters is there in the function call?
const unsigned int numpar = countParameters(tok);
2011-11-26 21:02:04 +01:00
if (numpar == 0) {
// Taking return value => it is not a noreturn function
if (tok->strAt(-1) == "=")
2014-02-15 16:17:25 +01:00
return nullptr;
// Function is not noreturn
if (tok->function() && tok->function()->functionScope) {
std::string temp;
if (!mSettings->library.isScopeNoReturn(tok->function()->functionScope->bodyEnd, &temp) && temp.empty())
return nullptr;
} else if (mSettings->library.isnotnoreturn(tok))
2014-02-15 16:17:25 +01:00
return nullptr;
return "callfunc";
}
2011-12-10 11:55:14 +01:00
unsigned int par = 0;
const bool dot(tok->previous()->str() == ".");
const bool eq(tok->previous()->str() == "=");
const Token *functok = tok;
2011-12-10 11:55:14 +01:00
tok = Token::findsimplematch(tok, "(");
if (tok)
tok = tok->next();
2011-12-10 11:55:14 +01:00
for (; tok; tok = tok->nextArgument()) {
++par;
2014-11-18 06:38:19 +01:00
if (Token::Match(tok, "%varid% [,()]", varid)) {
2011-12-10 11:55:14 +01:00
if (dot)
return "use";
const Function* function = functok->function();
if (!function)
2011-12-10 11:55:14 +01:00
return "use";
2011-12-10 11:55:14 +01:00
// how many parameters does the function want?
if (numpar != function->argCount()) // TODO: Handle default parameters
2011-12-10 11:55:14 +01:00
return "recursive";
2014-11-18 06:38:19 +01:00
if (!function->functionScope)
return "use";
const Variable* param = function->getArgumentVar(par-1);
if (!param || !param->nameToken())
return "use";
Token *func = getcode(function->functionScope->bodyStart->next(), callstack, param->declarationId(), alloctype, dealloctype, false, sz);
//simplifycode(func);
2011-12-10 11:55:14 +01:00
const Token *func_ = func;
while (func_ && func_->str() == ";")
func_ = func_->next();
const char *ret = nullptr;
2011-12-10 11:55:14 +01:00
/** @todo handle "goto" */
if (Token::findsimplematch(func_, "dealloc"))
ret = "dealloc";
else if (Token::findsimplematch(func_, "use"))
ret = "use";
else if (Token::findsimplematch(func_, "&use"))
ret = "&use";
TokenList::deleteTokens(func);
2011-12-10 11:55:14 +01:00
return ret;
}
2014-11-18 06:38:19 +01:00
if (Token::Match(tok, "& %varid% [,()]", varid)) {
const Function *func = functok->function();
if (func == nullptr)
continue;
2011-12-10 11:55:14 +01:00
AllocType a;
const char *ret = functionArgAlloc(func, par, a);
2011-12-10 11:55:14 +01:00
if (a != No) {
if (alloctype == No)
alloctype = a;
else if (alloctype != a)
alloctype = Many;
allocpar = true;
return ret;
}
}
if (Token::Match(tok, "%varid% . %name% [,)]", varid))
2011-12-10 11:55:14 +01:00
return "use";
}
return (eq || mSettings->experimental) ? nullptr : "callfunc";
}
template<typename T>
static void addtoken(Token **rettail, const Token *tok, T&& str)
2010-09-04 10:06:34 +02:00
{
(*rettail)->insertToken(str);
(*rettail) = (*rettail)->next();
(*rettail)->linenr(tok->linenr());
(*rettail)->fileIndex(tok->fileIndex());
}
Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list<const Token *> callstack, const unsigned int varid, CheckMemoryLeak::AllocType &alloctype, CheckMemoryLeak::AllocType &dealloctype, bool classmember, unsigned int sz)
{
// variables whose value depends on if(!var). If one of these variables
// is used in a if-condition then generate "ifv" instead of "if".
std::set<unsigned int> extravar;
// The first token should be ";"
Fix crash bug #8579 (#1238) * Added declaration for deletePrevious function * Added definition for deletePrevious function * Fixed crash from deleteThis invalidating pointers The crash was caused by deleteThis() invalidating the pointer to a constant variable usage. This happened when a usage followed an assignment. This fixes bug #8579. * Added tokensFront to match tokensBack This means deletePrevious can set the list's front if necessary. * Initialised tokensFront in appropriate places * Switched to using default Token constructor * Switched to using Token default constructor * Switched to using default constructor for Token * Added missing argument to Token constructor * Changed to use default constructor for Tokens * Switched to using default constructor for Tokens * Switched to using default constructor for Token * Added new test for deleting front Token Also made sure to use the correct constructor for Token in other tests. * Syntax error * Replaced tokensFront and tokensBack with a struct This decreases the size of the Token class for performance purposes. * Replaced tokensFront and tokensBack with a struct * Added tokensFrontBack to destructor * Reworked to use TokensBackFront struct Also ran astyle. * Reworked to use TokenList's TokensFrontBack member * Reworked to use TokensFrontBack struct * Reworked to use TokensFrontBack struct * Reworked to work with TokensFrontBack struct * Removed unnecessary scope operator * Added missing parentheses * Fixed syntax error * Removed unnecessary constructor * Default constructor now 0-initialises everything This is safer for not using a temporary TokensFrontBack object, and doesn't use delegating constructors which aren't supported yet. * Fixed unsafe null check * Added missing explicit keyword * Fixing stylistic nits Removed default constructor as it has been superseded by the single-argument constructor with a default argument value. Renamed listEnds to tokensFrontBack. Fixed if statement that was supposed to be adding safety but would actually cause a crash if tokensFrontBack was null. * Fixing stylistic nits Removed default constructor and replaced it with a single-argument constructor with a default value. * Fixing stylistic nits Renamed _listEnds to _tokensFrontBack. * Fixing stylistic nits Renamed _listEnds to _tokensFrontBack.
2018-05-25 07:15:05 +02:00
Token* rethead = new Token();
2010-09-04 10:06:34 +02:00
rethead->str(";");
rethead->linenr(tok->linenr());
rethead->fileIndex(tok->fileIndex());
Token* rettail = rethead;
int indentlevel = 0;
int parlevel = 0;
2011-10-13 20:53:06 +02:00
for (; tok; tok = tok->next()) {
if (tok->str() == "{") {
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, "{");
++indentlevel;
2011-10-13 20:53:06 +02:00
} else if (tok->str() == "}") {
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, "}");
if (indentlevel <= 0)
break;
--indentlevel;
}
2011-01-16 09:56:04 +01:00
else if (tok->str() == "(")
++parlevel;
else if (tok->str() == ")")
--parlevel;
if (parlevel == 0 && tok->str() == ";")
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, ";");
2011-01-16 09:56:04 +01:00
// Start of new statement.. check if the statement has anything interesting
2011-12-10 11:55:14 +01:00
if (varid > 0 && parlevel == 0 && Token::Match(tok, "[;{}]")) {
2011-01-16 09:56:04 +01:00
if (Token::Match(tok->next(), "[{};]"))
continue;
// function calls are interesting..
const Token *tok2 = tok;
if (Token::Match(tok2, "[{};] :: %name%"))
tok2 = tok2->next();
while (Token::Match(tok2->next(), "%name% ::|. %name%"))
2011-01-16 09:56:04 +01:00
tok2 = tok2->tokAt(2);
if (Token::Match(tok2->next(), "%name% ("))
2011-01-16 09:56:04 +01:00
;
else if (Token::Match(tok->next(), "continue|break|return|throw|goto|do|else"))
;
2011-10-13 20:53:06 +02:00
else {
const Token *skipToToken = nullptr;
2011-01-16 09:56:04 +01:00
// scan statement for interesting keywords / varid
2011-10-13 20:53:06 +02:00
for (tok2 = tok->next(); tok2; tok2 = tok2->next()) {
if (tok2->str() == ";") {
2011-01-16 09:56:04 +01:00
// nothing interesting found => skip this statement
skipToToken = tok2->previous();
break;
}
2011-01-16 09:57:56 +01:00
if (tok2->varId() == varid ||
2011-10-13 20:53:06 +02:00
tok2->str() == ":" || tok2->str() == "{" || tok2->str() == "}") {
2011-01-16 09:56:04 +01:00
break;
}
}
2011-01-16 09:57:56 +01:00
2011-10-13 20:53:06 +02:00
if (skipToToken) {
2011-01-16 09:56:04 +01:00
tok = skipToToken;
continue;
}
}
}
2011-10-13 20:53:06 +02:00
if (varid == 0) {
if (!callstack.empty() && Token::Match(tok, "[;{}] __cppcheck_lock|__cppcheck_unlock ( ) ;")) {
// Type of leak = Resource leak
alloctype = dealloctype = CheckMemoryLeak::File;
2011-10-13 20:53:06 +02:00
if (tok->next()->str() == "__cppcheck_lock") {
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, "alloc");
2011-10-13 20:53:06 +02:00
} else {
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, "dealloc");
2009-08-29 07:26:32 +02:00
}
tok = tok->tokAt(3);
continue;
}
2011-10-13 20:53:06 +02:00
if (Token::simpleMatch(tok, "if (")) {
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, "if");
tok = tok->next()->link();
continue;
}
2011-10-13 20:53:06 +02:00
} else {
2011-10-13 20:53:06 +02:00
if (Token::Match(tok, "%varid% = close ( %varid% )", varid)) {
addtoken(&rettail, tok, "dealloc");
addtoken(&rettail, tok, ";");
addtoken(&rettail, tok, "assign");
addtoken(&rettail, tok, ";");
tok = tok->tokAt(5);
continue;
}
// var = strcpy|.. ( var ,
2011-10-13 20:53:06 +02:00
if (Token::Match(tok, "[;{}] %varid% = memcpy|memmove|memset|strcpy|strncpy|strcat|strncat ( %varid% ,", varid)) {
tok = tok->linkAt(4);
continue;
}
if (Token::Match(tok->previous(), "[(;{}] %varid% =", varid) ||
Token::Match(tok, "asprintf|vasprintf ( & %varid% ,", varid)) {
CheckMemoryLeak::AllocType alloc;
if (Token::Match(tok, "asprintf|vasprintf (")) {
// todo: check how the return value is used.
2011-10-13 20:53:06 +02:00
if (!Token::Match(tok->previous(), "[;{}]")) {
TokenList::deleteTokens(rethead);
return nullptr;
}
alloc = Malloc;
tok = tok->next()->link();
2011-10-13 20:53:06 +02:00
} else {
alloc = getAllocationType(tok->tokAt(2), varid);
}
if (sz > 1 &&
Token::Match(tok->tokAt(2), "malloc ( %num% )") &&
2011-10-13 20:53:06 +02:00
(MathLib::toLongNumber(tok->strAt(4)) % long(sz)) != 0) {
mismatchSizeError(tok->tokAt(4), tok->strAt(4));
}
2011-10-13 20:53:06 +02:00
if (alloc == CheckMemoryLeak::No) {
alloc = getReallocationType(tok->tokAt(2), varid);
2011-10-13 20:53:06 +02:00
if (alloc != CheckMemoryLeak::No) {
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, "realloc");
addtoken(&rettail, tok, ";");
tok = tok->tokAt(2);
if (Token::Match(tok, "%name% ("))
2011-01-16 09:56:04 +01:00
tok = tok->next()->link();
continue;
}
}
// don't check classes..
2011-10-13 20:53:06 +02:00
if (alloc == CheckMemoryLeak::New) {
if (Token::Match(tok->tokAt(2), "new struct| %type% [(;]")) {
2011-08-07 01:10:15 +02:00
const int offset = tok->strAt(3) == "struct" ? 1 : 0;
if (isclass(tok->tokAt(3 + offset), varid)) {
alloc = No;
}
2011-10-13 20:53:06 +02:00
} else if (Token::Match(tok->tokAt(2), "new ( nothrow ) struct| %type%")) {
2011-08-07 01:10:15 +02:00
const int offset = tok->strAt(6) == "struct" ? 1 : 0;
if (isclass(tok->tokAt(6 + offset), varid)) {
alloc = No;
}
2011-10-13 20:53:06 +02:00
} else if (Token::Match(tok->tokAt(2), "new ( std :: nothrow ) struct| %type%")) {
2011-08-07 01:10:15 +02:00
const int offset = tok->strAt(8) == "struct" ? 1 : 0;
if (isclass(tok->tokAt(8 + offset), varid)) {
alloc = No;
}
}
if (Token::simpleMatch(tok->next(), "= new")) {
tok = tok->tokAt(2);
while (Token::Match(tok->next(), "%name%|::|(|[")) {
if (Token::Match(tok->next(), "(|["))
tok = tok->linkAt(1);
else
tok = tok->next();
}
}
if (alloc == No && alloctype == No)
alloctype = CheckMemoryLeak::New;
}
2011-10-13 20:53:06 +02:00
if (alloc != No) {
addtoken(&rettail, tok, "alloc");
if (alloctype != No && alloctype != alloc)
alloc = Many;
2011-10-13 20:53:06 +02:00
if (alloc != Many && dealloctype != No && dealloctype != Many && dealloctype != alloc) {
callstack.push_back(tok);
mismatchAllocDealloc(callstack, Token::findmatch(mTokenizer->tokens(), "%varid%", varid)->str());
callstack.pop_back();
}
alloctype = alloc;
if (Token::Match(tok, "%name% = %type% (")) {
tok = tok->linkAt(3);
continue;
}
}
// assignment..
2011-10-13 20:53:06 +02:00
else {
// is the pointer in rhs?
bool rhs = false;
bool trailingSemicolon = false;
bool used = false;
2011-10-13 20:53:06 +02:00
for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next()) {
if (tok2->str() == ";") {
trailingSemicolon = true;
if (rhs)
tok = tok2;
break;
}
2014-11-18 06:38:19 +01:00
if (!used && !rhs) {
if (Token::Match(tok2, "[=+(,] %varid%", varid)) {
2014-11-18 06:38:19 +01:00
if (Token::Match(tok2, "[(,]")) {
used = true;
addtoken(&rettail, tok, "use");
addtoken(&rettail, tok, ";");
}
rhs = true;
}
}
}
if (!used) {
if (!rhs)
addtoken(&rettail, tok, "assign");
else {
addtoken(&rettail, tok, "use_");
if (trailingSemicolon)
addtoken(&rettail, tok, ";");
}
}
continue;
}
}
if (Token::Match(tok->previous(), "%op%|;|{|}|) ::| %name%") || (Token::Match(tok->previous(), "( ::| %name%") && (!rettail || rettail->str() != "loop"))) {
if (tok->str() == "::")
tok = tok->next();
if (Token::Match(tok, "%varid% ?", varid))
tok = tok->tokAt(2);
AllocType dealloc = getDeallocationType(tok, varid);
if (dealloc != No && tok->str() == "fcloseall" && alloctype != dealloc)
;
2011-10-13 20:53:06 +02:00
else if (dealloc != No) {
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, "dealloc");
if (dealloctype != No && dealloctype != dealloc)
dealloc = Many;
2011-10-13 20:53:06 +02:00
if (dealloc != Many && alloctype != No && alloctype != Many && alloctype != dealloc) {
callstack.push_back(tok);
mismatchAllocDealloc(callstack, Token::findmatch(mTokenizer->tokens(), "%varid%", varid)->str());
callstack.pop_back();
}
dealloctype = dealloc;
2011-01-16 09:57:56 +01:00
2011-01-16 09:56:04 +01:00
if (tok->strAt(2) == "(")
tok = tok->linkAt(2);
continue;
}
}
// if else switch
2011-10-13 20:53:06 +02:00
if (Token::simpleMatch(tok, "if (")) {
if (alloctype == Fd) {
if (ifvar(tok, varid, ">", "-1") ||
ifvar(tok, varid, ">=", "0") ||
ifvar(tok, varid, ">", "0") ||
ifvar(tok, varid, "!=", "-1")) {
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, "if(var)");
tok = tok->next()->link();
continue;
} else if (ifvar(tok, varid, "==", "-1") ||
ifvar(tok, varid, "<", "0")) {
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, "if(!var)");
tok = tok->next()->link();
continue;
}
2009-02-01 16:47:36 +01:00
}
if (ifvar(tok, varid, "!=", "0")) {
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, "if(var)");
// Make sure the "use" will not be added
tok = tok->next()->link();
2011-01-16 09:56:04 +01:00
continue;
} else if (ifvar(tok, varid, "==", "0")) {
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, "if(!var)");
// parse the if-body.
// if a variable is assigned then add variable to "extravar".
2011-10-13 20:53:06 +02:00
for (const Token *tok2 = tok->next()->link()->tokAt(2); tok2; tok2 = tok2->next()) {
if (tok2->str() == "{")
tok2 = tok2->link();
else if (tok2->str() == "}")
break;
else if (Token::Match(tok2, "%var% ="))
extravar.insert(tok2->varId());
}
2011-01-16 09:56:04 +01:00
tok = tok->next()->link();
continue;
2011-10-13 20:53:06 +02:00
} else {
// Check if the condition depends on var or extravar somehow..
bool dep = false;
const Token* const end = tok->linkAt(1);
for (const Token *tok2 = tok->next(); tok2 != end; tok2 = tok2->next()) {
if (Token::Match(tok2, "close|pclose|fclose|closedir ( %varid% )", varid)) {
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, "dealloc");
addtoken(&rettail, tok, ";");
dep = true;
break;
} else if (alloctype == Fd && Token::Match(tok2, "%varid% !=|>=", varid)) {
dep = true;
} else if (Token::Match(tok2, "! %varid%", varid)) {
dep = true;
} else if (Token::Match(tok2, "%name% (") && !test_white_list(tok2->str(), mSettings, tokenizer->isCPP())) {
bool use = false;
for (const Token *tok3 = tok2->tokAt(2); tok3; tok3 = tok3->nextArgument()) {
if (Token::Match(tok3->previous(), "(|, &| %varid% ,|)", varid)) {
use = true;
break;
}
}
2011-10-13 20:53:06 +02:00
if (use) {
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, "use");
addtoken(&rettail, tok, ";");
dep = false;
break;
}
} else if (tok2->varId() && extravar.find(tok2->varId()) != extravar.end()) {
dep = true;
} else if (tok2->varId() == varid &&
(tok2->next()->isConstOp() || tok2->previous()->isConstOp()))
dep = true;
}
if (notvar(tok->next()->astOperand2(), varid))
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, "if(!var)");
else
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, (dep ? "ifv" : "if"));
2011-01-16 09:56:04 +01:00
tok = tok->next()->link();
continue;
}
}
}
2011-10-13 20:53:06 +02:00
if ((tok->str() == "else") || (tok->str() == "switch")) {
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, tok->str());
if (Token::simpleMatch(tok, "switch ("))
2011-01-16 09:56:04 +01:00
tok = tok->next()->link();
continue;
}
2011-10-13 20:53:06 +02:00
if ((tok->str() == "case")) {
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, "case");
addtoken(&rettail, tok, ";");
2011-01-16 09:56:04 +01:00
if (Token::Match(tok, "case %any% :"))
tok = tok->tokAt(2);
continue;
}
2011-10-13 20:53:06 +02:00
if ((tok->str() == "default")) {
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, "default");
addtoken(&rettail, tok, ";");
2011-01-16 09:56:04 +01:00
continue;
}
// Loops..
2011-10-13 20:53:06 +02:00
else if ((tok->str() == "for") || (tok->str() == "while")) {
const Token* const end = tok->linkAt(1);
if ((Token::simpleMatch(tok, "while (") && alwaysTrue(tok->next()->astOperand2())) ||
2011-10-13 20:53:06 +02:00
Token::simpleMatch(tok, "for ( ; ; )")) {
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, "while1");
tok = end;
continue;
}
2011-10-13 20:53:06 +02:00
else if (varid && getDeallocationType(tok->tokAt(2), varid) != No) {
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, "dealloc");
addtoken(&rettail, tok, ";");
}
2011-10-13 20:53:06 +02:00
else if (alloctype == Fd && varid) {
if (Token::Match(tok, "while ( 0 <= %varid% )", varid) ||
Token::Match(tok, "while ( %varid% >= 0 )", varid) ||
Token::Match(tok, "while ( %varid% != -1 )", varid) ||
Token::Match(tok, "while ( -1 != %varid% )", varid)) {
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, "while(var)");
tok = end;
continue;
2011-10-13 20:53:06 +02:00
} else if (Token::Match(tok, "while ( %varid% == -1 )", varid) ||
Token::Match(tok, "while ( -1 == %varid% )", varid) ||
Token::Match(tok, "while ( %varid% < 0 )", varid) ||
Token::Match(tok, "while ( 0 > %varid% )", varid)) {
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, "while(!var)");
tok = end;
continue;
}
}
2011-10-13 20:53:06 +02:00
else if (varid && Token::Match(tok, "while ( %varid% )", varid)) {
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, "while(var)");
tok = end;
continue;
} else if (varid && Token::simpleMatch(tok, "while (") && notvar(tok->next()->astOperand2(), varid)) {
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, "while(!var)");
tok = end;
continue;
}
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, "loop");
2011-01-16 09:57:56 +01:00
if (varid > 0 && notvar(tok->next()->astOperand2(), varid))
addtoken(&rettail, tok, "!var");
2011-01-16 09:57:56 +01:00
2011-01-16 09:56:04 +01:00
continue;
}
2011-10-13 20:53:06 +02:00
if ((tok->str() == "do")) {
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, "do");
2011-01-16 09:56:04 +01:00
continue;
}
// continue / break..
2011-12-10 11:55:14 +01:00
else if (tok->str() == "continue") {
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, "continue");
2011-10-13 20:53:06 +02:00
} else if (tok->str() == "break") {
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, "break");
2011-10-13 20:53:06 +02:00
} else if (tok->str() == "goto") {
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, "goto");
}
// Return..
2011-10-13 20:53:06 +02:00
else if (tok->str() == "return") {
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, "return");
2011-10-13 20:53:06 +02:00
if (varid == 0) {
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, ";");
while (tok && tok->str() != ";")
tok = tok->next();
if (!tok)
break;
continue;
}
// Returning a auto_ptr of this allocated variable..
2011-10-13 20:53:06 +02:00
if (Token::simpleMatch(tok->next(), "std :: auto_ptr <")) {
const Token *tok2 = tok->linkAt(4);
2011-10-13 20:53:06 +02:00
if (Token::Match(tok2, "> ( %varid% )", varid)) {
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, "use");
tok = tok2->tokAt(3);
}
}
2011-10-13 20:53:06 +02:00
else if (varid && Token::Match(tok, "return strcpy|strncpy|memcpy ( %varid%", varid)) {
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, "use");
tok = tok->tokAt(2);
}
2011-10-13 20:53:06 +02:00
else {
bool use = false;
2015-09-11 16:26:33 +02:00
std::stack<const Token *> functions;
2011-10-13 20:53:06 +02:00
for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next()) {
if (tok2->str() == ";") {
tok = tok2;
break;
}
if (tok2->str() == "(")
2015-09-11 16:26:33 +02:00
functions.push(tok2->previous());
else if (!functions.empty() && tok2->str() == ")")
functions.pop();
2011-10-13 20:53:06 +02:00
if (tok2->varId() == varid) {
// Read data..
if (!Token::Match(tok2->previous(), "&|(") &&
tok2->strAt(1) == "[") {
2015-09-11 16:26:33 +02:00
;
} else if (functions.empty() ||
!test_white_list(functions.top()->str(), mSettings, tokenizer->isCPP()) ||
2017-10-08 07:54:39 +02:00
getDeallocationType(functions.top(),varid) != AllocType::No) {
use = true;
}
}
}
if (use)
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, "use");
addtoken(&rettail, tok, ";");
}
}
// throw..
else if (tokenizer->isCPP() && Token::Match(tok, "try|throw|catch")) {
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, tok->str());
if (tok->strAt(1) == "(")
tok = tok->next()->link();
}
// Assignment..
2011-10-13 20:53:06 +02:00
if (varid) {
if (Token::simpleMatch(tok, "= {")) {
const Token* const end2 = tok->linkAt(1);
bool use = false;
for (const Token *tok2 = tok; tok2 != end2; tok2 = tok2->next()) {
if (tok2->varId() == varid) {
use = true;
break;
}
}
2011-10-13 20:53:06 +02:00
if (use) {
addtoken(&rettail, tok, "use");
addtoken(&rettail, tok, ";");
tok = tok->next()->link();
continue;
}
}
if (Token::Match(tok, "& %name% = %varid% ;", varid)) {
while (rethead->next())
rethead->deleteNext();
return rethead;
}
if (Token::Match(tok, "[)=] %varid% [+;)]", varid) ||
(Token::Match(tok, "%name% + %varid%", varid) &&
tok->strAt(3) != "[" &&
tok->strAt(3) != ".") ||
Token::Match(tok, "<< %varid% ;", varid) ||
Token::Match(tok, "= strcpy|strcat|memmove|memcpy ( %varid% ,", varid) ||
Token::Match(tok, "[;{}] %name% [ %varid% ]", varid)) {
addtoken(&rettail, tok, "use");
2015-03-24 08:22:26 +01:00
} else if (Token::Match(tok->previous(), ";|{|}|=|(|,|%cop% %varid% .|[", varid)) {
// warning is written for "dealloc ; use_ ;".
// but this use doesn't affect the leak-checking
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, "use_");
}
}
// Investigate function calls..
if (Token::Match(tok, "%name% (")) {
// A function call should normally be followed by ";"
2011-10-13 20:53:06 +02:00
if (Token::simpleMatch(tok->next()->link(), ") {")) {
if (!Token::Match(tok, "if|for|while|switch")) {
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, "exit");
addtoken(&rettail, tok, ";");
tok = tok->next()->link();
continue;
}
}
// Calling setjmp / longjmp => bail out
2011-10-13 20:53:06 +02:00
else if (Token::Match(tok, "setjmp|longjmp")) {
while (rethead->next())
rethead->deleteNext();
return rethead;
}
// Inside class function.. if the var is passed as a parameter then
// just add a "::use"
2010-12-15 18:45:53 +01:00
// The "::use" means that a member function was probably called but it wasn't analysed further
2011-10-13 20:53:06 +02:00
else if (classmember) {
if (mSettings->library.isnoreturn(tok))
addtoken(&rettail, tok, "exit");
else if (!test_white_list(tok->str(), mSettings, tokenizer->isCPP())) {
const Token* const end2 = tok->linkAt(1);
for (const Token *tok2 = tok->tokAt(2); tok2 != end2; tok2 = tok2->next()) {
2011-10-13 20:53:06 +02:00
if (tok2->varId() == varid) {
addtoken(&rettail, tok, "::use");
break;
}
}
}
}
2011-10-13 20:53:06 +02:00
else {
if (varid > 0 && Token::Match(tok, "%name% ( close|fclose|pclose ( %varid% ) ) ;", varid)) {
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, "dealloc");
tok = tok->next()->link();
continue;
}
bool allocpar = false;
const char *str = call_func(tok, callstack, varid, alloctype, dealloctype, allocpar, sz);
2011-10-13 20:53:06 +02:00
if (str) {
if (allocpar) {
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, str);
tok = tok->next()->link();
2011-10-13 20:53:06 +02:00
} else if (varid == 0 || str != std::string("alloc")) {
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, str);
2011-10-13 20:53:06 +02:00
} else if (Token::Match(tok->tokAt(-2), "%varid% =", varid)) {
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, str);
}
2011-10-13 20:53:06 +02:00
} else if (varid > 0 &&
getReallocationType(tok, varid) != No &&
tok->tokAt(2)->varId() == varid) {
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, "if");
addtoken(&rettail, tok, "{");
addtoken(&rettail, tok, "dealloc");
addtoken(&rettail, tok, ";");
addtoken(&rettail, tok, "}");
tok = tok->next()->link();
continue;
}
}
}
// Callback..
if (Token::Match(tok, "( *| %name%") && Token::simpleMatch(tok->link(),") (")) {
2011-03-06 09:42:16 +01:00
const Token *tok2 = tok->next();
if (tok2->str() == "*")
tok2 = tok2->next();
tok2 = tok2->next();
while (Token::Match(tok2, ". %name%"))
2011-03-06 09:42:16 +01:00
tok2 = tok2->tokAt(2);
2011-10-13 20:53:06 +02:00
if (Token::simpleMatch(tok2, ") (")) {
for (; tok2; tok2 = tok2->next()) {
if (Token::Match(tok2, "[;{]"))
break;
2011-10-13 20:53:06 +02:00
else if (tok2->varId() == varid) {
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, "use");
break;
}
}
}
}
// Linux lists..
2014-11-07 07:44:12 +01:00
if (varid > 0 && Token::Match(tok, "[=(,] & (| %varid% [.[,)]", varid)) {
// Is variable passed to a "leak-ignore" function?
bool leakignore = false;
if (Token::Match(tok, "[(,]")) {
const Token *parent = tok;
while (parent && parent->str() != "(")
parent = parent->astParent();
if (parent && parent->astOperand1() && parent->astOperand1()->isName()) {
const std::string &functionName = parent->astOperand1()->str();
if (mSettings->library.isLeakIgnore(functionName))
2014-11-07 07:44:12 +01:00
leakignore = true;
}
}
// Not passed to "leak-ignore" function, add "&use".
if (!leakignore)
addtoken(&rettail, tok, "&use");
}
}
2011-10-13 20:53:06 +02:00
for (Token *tok1 = rethead; tok1; tok1 = tok1->next()) {
if (Token::simpleMatch(tok1, "callfunc alloc ;")) {
tok1->deleteThis();
tok1->insertToken("use");
tok1->insertToken(";");
}
}
return rethead;
}
void CheckMemoryLeakInFunction::simplifycode(Token *tok) const
{
if (mTokenizer->isCPP()) {
2011-01-01 12:27:57 +01:00
// Replace "throw" that is not in a try block with "return"
int indentlevel = 0;
int trylevel = -1;
2011-10-13 20:53:06 +02:00
for (Token *tok2 = tok; tok2; tok2 = tok2->next()) {
2011-01-01 12:27:57 +01:00
if (tok2->str() == "{")
++indentlevel;
2011-10-13 20:53:06 +02:00
else if (tok2->str() == "}") {
2011-01-01 12:27:57 +01:00
--indentlevel;
if (indentlevel <= trylevel)
trylevel = -1;
2011-10-13 20:53:06 +02:00
} else if (trylevel == -1 && tok2->str() == "try")
2011-01-01 12:27:57 +01:00
trylevel = indentlevel;
else if (trylevel == -1 && tok2->str() == "throw")
tok2->str("return");
}
}
const bool printExperimental = mSettings->experimental;
// Insert extra ";"
2011-10-13 20:53:06 +02:00
for (Token *tok2 = tok; tok2; tok2 = tok2->next()) {
if (!tok2->previous() || Token::Match(tok2->previous(), "[;{}]")) {
if (Token::Match(tok2, "assign|callfunc|use assign|callfunc|use|}")) {
tok2->insertToken(";");
}
}
}
// remove redundant braces..
2011-10-13 20:53:06 +02:00
for (Token *start = tok; start; start = start->next()) {
if (Token::simpleMatch(start, "; {")) {
// the "link" doesn't work here. Find the end brace..
unsigned int indent = 0;
2011-10-13 20:53:06 +02:00
for (Token *end = start; end; end = end->next()) {
if (end->str() == "{")
++indent;
2011-10-13 20:53:06 +02:00
else if (end->str() == "}") {
if (indent <= 1) {
// If the start/end braces are redundant, delete them
2011-10-13 20:53:06 +02:00
if (indent == 1 && Token::Match(end->previous(), "[;{}] } %any%")) {
start->deleteNext();
end->deleteThis();
}
break;
}
--indent;
}
}
}
}
// reduce the code..
// it will be reduced in N passes. When a pass completes without any
// simplifications the loop is done.
bool done = false;
2011-10-13 20:53:06 +02:00
while (! done) {
//tok->printOut("simplifycode loop..");
done = true;
// reduce callfunc
2011-10-13 20:53:06 +02:00
for (Token *tok2 = tok; tok2; tok2 = tok2->next()) {
if (tok2->str() == "callfunc") {
if (!Token::Match(tok2->previous(), "[;{}] callfunc ; }"))
tok2->deleteThis();
}
}
// If the code starts with "if return ;" then remove it
2011-10-13 20:53:06 +02:00
if (Token::Match(tok, ";| if return ;")) {
tok->deleteNext();
tok->deleteThis();
if (tok->str() == "return")
tok->deleteThis();
if (tok->strAt(1) == "else")
tok->deleteNext();
}
// simplify "while1" contents..
2011-10-13 20:53:06 +02:00
for (Token *tok2 = tok; tok2; tok2 = tok2->next()) {
if (Token::simpleMatch(tok2, "while1 {")) {
2010-04-06 22:17:23 +02:00
unsigned int innerIndentlevel = 0;
2011-10-13 20:53:06 +02:00
for (Token *tok3 = tok2->tokAt(2); tok3; tok3 = tok3->next()) {
if (tok3->str() == "{")
2010-04-06 22:17:23 +02:00
++innerIndentlevel;
2011-10-13 20:53:06 +02:00
else if (tok3->str() == "}") {
2010-04-06 22:17:23 +02:00
if (innerIndentlevel == 0)
break;
2010-04-06 22:17:23 +02:00
--innerIndentlevel;
}
2011-10-13 20:53:06 +02:00
while (innerIndentlevel == 0 && Token::Match(tok3, "[{};] if|ifv|else { continue ; }")) {
tok3->deleteNext(5);
if (tok3->strAt(1) == "else")
tok3->deleteNext();
}
}
2011-10-13 20:53:06 +02:00
if (Token::simpleMatch(tok2, "while1 { if { dealloc ; return ; } }")) {
tok2->str(";");
tok2->deleteNext(3);
tok2->tokAt(4)->deleteNext(2);
}
}
}
// Main inner simplification loop
2014-02-15 16:17:25 +01:00
for (Token *tok2 = tok; tok2; tok2 = tok2 ? tok2->next() : nullptr) {
// Delete extra ";"
2011-10-13 20:53:06 +02:00
while (Token::Match(tok2, "[;{}] ;")) {
tok2->deleteNext();
done = false;
}
// Replace "{ }" with ";"
2011-10-13 20:53:06 +02:00
if (Token::simpleMatch(tok2->next(), "{ }")) {
tok2->deleteNext(2);
tok2->insertToken(";");
done = false;
}
// Delete braces around a single instruction..
if (Token::Match(tok2->next(), "{ %name% ; }")) {
tok2->deleteNext();
tok2->tokAt(2)->deleteNext();
done = false;
}
if (Token::Match(tok2->next(), "{ %name% %name% ; }")) {
tok2->deleteNext();
tok2->tokAt(3)->deleteNext();
done = false;
}
// Reduce "if if|callfunc" => "if"
2011-10-13 20:53:06 +02:00
else if (Token::Match(tok2, "if if|callfunc")) {
tok2->deleteNext();
done = false;
}
// outer/inner if blocks. Remove outer condition..
2011-10-13 20:53:06 +02:00
else if (Token::Match(tok2->next(), "if|if(var) { if return use ; }")) {
tok2->deleteNext(2);
tok2->tokAt(4)->deleteNext();
done = false;
}
2011-10-13 20:53:06 +02:00
else if (tok2->next() && tok2->next()->str() == "if") {
// Delete empty if that is not followed by an else
2011-10-13 20:53:06 +02:00
if (Token::Match(tok2->next(), "if ; !!else")) {
tok2->deleteNext();
done = false;
}
// Reduce "if X ; else X ;" => "X ;"
else if (Token::Match(tok2->next(), "if %name% ; else %name% ;") &&
tok2->strAt(2) == tok2->strAt(5)) {
tok2->deleteNext(4);
done = false;
}
2010-11-27 11:09:42 +01:00
// Reduce "if continue ; if continue ;" => "if continue ;"
2011-10-13 20:53:06 +02:00
else if (Token::simpleMatch(tok2->next(), "if continue ; if continue ;")) {
tok2->deleteNext(3);
2010-11-27 11:09:42 +01:00
done = false;
}
// Reduce "if return ; alloc ;" => "alloc ;"
2011-10-13 20:53:06 +02:00
else if (Token::Match(tok2, "[;{}] if return ; alloc|return ;")) {
tok2->deleteNext(3);
done = false;
}
// "[;{}] if alloc ; else return ;" => "[;{}] alloc ;"
2011-10-13 20:53:06 +02:00
else if (Token::Match(tok2, "[;{}] if alloc ; else return ;")) {
// Remove "if"
tok2->deleteNext();
// Remove "; else return"
tok2->next()->deleteNext(3);
done = false;
}
// Reduce "if ; else %name% ;" => "if %name% ;"
else if (Token::Match(tok2->next(), "if ; else %name% ;")) {
tok2->next()->deleteNext(2);
done = false;
}
// Reduce "if ; else" => "if"
2011-10-13 20:53:06 +02:00
else if (Token::simpleMatch(tok2->next(), "if ; else")) {
tok2->next()->deleteNext(2);
done = false;
}
// Reduce "if return ; else|if return|continue ;" => "if return ;"
2011-10-13 20:53:06 +02:00
else if (Token::Match(tok2->next(), "if return ; else|if return|continue|break ;")) {
tok2->tokAt(3)->deleteNext(3);
done = false;
}
// Reduce "if continue|break ; else|if return ;" => "if return ;"
2011-10-13 20:53:06 +02:00
else if (Token::Match(tok2->next(), "if continue|break ; if|else return ;")) {
tok2->next()->deleteNext(3);
done = false;
}
2010-11-27 11:09:42 +01:00
// Remove "else" after "if continue|break|return"
2011-10-13 20:53:06 +02:00
else if (Token::Match(tok2->next(), "if continue|break|return ; else")) {
tok2->tokAt(3)->deleteNext();
2010-11-27 11:09:42 +01:00
done = false;
}
// Delete "if { dealloc|assign|use ; return ; }"
else if (Token::Match(tok2, "[;{}] if { dealloc|assign|use ; return ; }") &&
!Token::findmatch(tok, "if {| alloc ;")) {
tok2->deleteNext(7);
if (tok2->strAt(1) == "else")
tok2->deleteNext();
done = false;
}
// Remove "if { dealloc ; callfunc ; } !!else|return"
else if (Token::Match(tok2->next(), "if { dealloc|assign ; callfunc ; }") &&
2011-10-13 20:53:06 +02:00
!Token::Match(tok2->tokAt(8), "else|return")) {
tok2->deleteNext(7);
done = false;
}
continue;
}
// Reduce "alloc while(!var) alloc ;" => "alloc ;"
2011-10-13 20:53:06 +02:00
if (Token::Match(tok2, "[;{}] alloc ; while(!var) alloc ;")) {
tok2->deleteNext(3);
done = false;
}
// Reduce "ifv return;" => "if return use;"
2011-10-13 20:53:06 +02:00
if (Token::simpleMatch(tok2, "ifv return ;")) {
tok2->str("if");
tok2->next()->insertToken("use");
done = false;
}
// Reduce "if(var) dealloc ;" and "if(var) use ;" that is not followed by an else..
2011-10-13 20:53:06 +02:00
if (Token::Match(tok2, "[;{}] if(var) assign|dealloc|use ; !!else")) {
tok2->deleteNext();
done = false;
}
// Reduce "; if(!var) alloc ; !!else" => "; dealloc ; alloc ;"
2011-10-13 20:53:06 +02:00
if (Token::Match(tok2, "; if(!var) alloc ; !!else")) {
// Remove the "if(!var)"
tok2->deleteNext();
// Insert "dealloc ;" before the "alloc ;"
tok2->insertToken(";");
tok2->insertToken("dealloc");
done = false;
}
// Reduce "if(!var) exit ;" => ";"
if (Token::simpleMatch(tok2, "; if(!var) exit ;")) {
tok2->deleteNext(2);
done = false;
}
// Reduce "if* ;"..
2011-10-13 20:53:06 +02:00
if (Token::Match(tok2->next(), "if(var)|if(!var)|ifv ;")) {
// Followed by else..
if (tok2->strAt(3) == "else") {
tok2 = tok2->next();
if (tok2->str() == "if(var)")
tok2->str("if(!var)");
else if (tok2->str() == "if(!var)")
tok2->str("if(var)");
// remove the "; else"
tok2->deleteNext(2);
2011-10-13 20:53:06 +02:00
} else {
// remove the "if*"
tok2->deleteNext();
}
done = false;
}
// Reduce "else ;" => ";"
2011-10-13 20:53:06 +02:00
if (Token::simpleMatch(tok2->next(), "else ;")) {
tok2->deleteNext();
done = false;
}
// Reduce "while1 continue| ;" => "use ;"
2011-10-13 20:53:06 +02:00
if (Token::Match(tok2, "while1 if| continue| ;")) {
tok2->str("use");
while (tok2->strAt(1) != ";")
tok2->deleteNext();
done = false;
}
// Reduce "while1 if break ;" => ";"
2011-10-13 20:53:06 +02:00
if (Token::simpleMatch(tok2, "while1 if break ;")) {
tok2->str(";");
tok2->deleteNext(2);
done = false;
}
// Delete if block: "alloc; if return use ;"
2011-10-13 20:53:06 +02:00
if (Token::Match(tok2, "alloc ; if return use ; !!else")) {
tok2->deleteNext(4);
done = false;
}
// Reduce "alloc|dealloc|use|callfunc ; exit ;" => "; exit ;"
2011-10-13 20:53:06 +02:00
if (Token::Match(tok2, "[;{}] alloc|dealloc|use|callfunc ; exit ;")) {
tok2->deleteNext();
done = false;
}
// Reduce "alloc|dealloc|use ; if(var) exit ;"
2011-10-13 20:53:06 +02:00
if (Token::Match(tok2, "alloc|dealloc|use ; if(var) exit ;")) {
tok2->deleteThis();
done = false;
}
// Remove "if exit ;"
2011-10-13 20:53:06 +02:00
if (Token::simpleMatch(tok2, "if exit ;")) {
tok2->deleteNext();
tok2->deleteThis();
done = false;
}
// Remove the "if break|continue ;" that follows "dealloc ; alloc ;"
if (!printExperimental && Token::Match(tok2, "dealloc ; alloc ; if break|continue ;")) {
tok2->tokAt(3)->deleteNext(2);
done = false;
}
// if break ; break ; => break ;
2011-10-13 20:53:06 +02:00
if (Token::Match(tok2->previous(), "[;{}] if break ; break ;")) {
tok2->deleteNext(3);
done = false;
}
// Reduce "do { dealloc ; alloc ; } while(var) ;" => ";"
2011-10-13 20:53:06 +02:00
if (Token::simpleMatch(tok2->next(), "do { dealloc ; alloc ; } while(var) ;")) {
tok2->deleteNext(8);
done = false;
}
// Ticket #7745
// Delete "if (!var) { alloc ; dealloc }" blocks
if (Token::simpleMatch(tok2->next(), "if(!var) { alloc ; dealloc ; }")) {
tok2->deleteNext(7);
done = false;
}
// Reduce "do { alloc ; } " => "alloc ;"
/** @todo If the loop "do { alloc ; }" can be executed twice, reduce it to "loop alloc ;" */
2011-10-13 20:53:06 +02:00
if (Token::simpleMatch(tok2->next(), "do { alloc ; }")) {
tok2->deleteNext(2);
tok2->tokAt(2)->deleteNext();
done = false;
}
// Reduce "loop break ; => ";"
2011-10-13 20:53:06 +02:00
if (Token::Match(tok2->next(), "loop break|continue ;")) {
tok2->deleteNext(2);
done = false;
}
// Reduce "loop|do ;" => ";"
2011-10-13 20:53:06 +02:00
if (Token::Match(tok2, "loop|do ;")) {
tok2->deleteThis();
done = false;
}
// Reduce "loop if break|continue ; !!else" => ";"
2011-10-13 20:53:06 +02:00
if (Token::Match(tok2->next(), "loop if break|continue ; !!else")) {
tok2->deleteNext(3);
done = false;
}
// Reduce "loop { if break|continue ; !!else" => "loop {"
2011-10-13 20:53:06 +02:00
if (Token::Match(tok2, "loop { if break|continue ; !!else")) {
tok2->next()->deleteNext(3);
done = false;
}
// Replace "do ; loop ;" with ";"
2011-10-13 20:53:06 +02:00
if (Token::simpleMatch(tok2, "; loop ;")) {
tok2->deleteNext(2);
done = false;
}
// Replace "loop loop .." with "loop .."
2011-10-13 20:53:06 +02:00
if (Token::simpleMatch(tok2, "loop loop")) {
tok2->deleteThis();
done = false;
}
2009-01-17 08:55:40 +01:00
// Replace "loop if return ;" with "if return ;"
2011-10-13 20:53:06 +02:00
if (Token::simpleMatch(tok2->next(), "loop if return")) {
tok2->deleteNext();
2009-01-17 08:55:40 +01:00
done = false;
}
// Reduce "loop|while1 { dealloc ; alloc ; }"
2011-10-13 20:53:06 +02:00
if (Token::Match(tok2, "loop|while1 { dealloc ; alloc ; }")) {
// delete "{"
tok2->deleteNext();
// delete "loop|while1"
tok2->deleteThis();
// delete "}"
tok2->tokAt(3)->deleteNext();
done = false;
}
// loop { use ; callfunc ; } => use ;
// assume that the "callfunc" is not noreturn
2011-10-13 20:53:06 +02:00
if (Token::simpleMatch(tok2, "loop { use ; callfunc ; }")) {
tok2->deleteNext(6);
tok2->str("use");
tok2->insertToken(";");
done = false;
}
// Delete if block in "alloc ; if(!var) return ;"
if (Token::simpleMatch(tok2, "alloc ; if(!var) return ;")) {
tok2->deleteNext(3);
done = false;
}
// Reduce "[;{}] return use ; %name%" => "[;{}] return use ;"
if (Token::Match(tok2, "[;{}] return use ; %name%")) {
tok2->tokAt(3)->deleteNext();
done = false;
}
// Reduce "if(var) return use ;" => "return use ;"
2011-10-13 20:53:06 +02:00
if (Token::Match(tok2->next(), "if(var) return use ; !!else")) {
tok2->deleteNext();
done = false;
}
// malloc - realloc => alloc ; dealloc ; alloc ;
// Reduce "[;{}] alloc ; dealloc ; alloc ;" => "[;{}] alloc ;"
2011-10-13 20:53:06 +02:00
if (Token::Match(tok2, "[;{}] alloc ; dealloc ; alloc ;")) {
tok2->deleteNext(4);
done = false;
}
// use; dealloc; => dealloc;
2011-10-13 20:53:06 +02:00
if (Token::Match(tok2, "[;{}] use ; dealloc ;")) {
tok2->deleteNext(2);
done = false;
}
2010-09-11 21:32:21 +02:00
// use use => use
while (Token::simpleMatch(tok2, "use use")) {
tok2->deleteNext();
2010-09-11 21:32:21 +02:00
done = false;
}
// use use_ => use
if (Token::simpleMatch(tok2, "use use_")) {
tok2->deleteNext();
done = false;
}
// use_ use => use
if (Token::simpleMatch(tok2, "use_ use")) {
tok2->deleteThis();
done = false;
}
// use & use => use
while (Token::simpleMatch(tok2, "use & use")) {
tok2->deleteNext(2);
done = false;
}
// & use use => use
while (Token::simpleMatch(tok2, "& use use")) {
tok2->deleteThis();
tok2->deleteThis();
done = false;
}
// use; if| use; => use;
2011-10-13 20:53:06 +02:00
while (Token::Match(tok2, "[;{}] use ; if| use ;")) {
Token *t = tok2->tokAt(2);
t->deleteNext(2+(t->str()=="if" ? 1 : 0));
done = false;
}
// Delete first part in "use ; return use ;"
2011-10-13 20:53:06 +02:00
if (Token::Match(tok2, "[;{}] use ; return use ;")) {
tok2->deleteNext(2);
done = false;
}
// try/catch
2011-10-13 20:53:06 +02:00
if (Token::simpleMatch(tok2, "try ; catch exit ;")) {
tok2->deleteNext(3);
tok2->deleteThis();
done = false;
}
// Delete second case in "case ; case ;"
2011-10-13 20:53:06 +02:00
while (Token::simpleMatch(tok2, "case ; case ;")) {
tok2->deleteNext(2);
done = false;
}
// Replace switch with if (if not complicated)
2011-10-13 20:53:06 +02:00
if (Token::simpleMatch(tok2, "switch {")) {
// Right now, I just handle if there are a few case and perhaps a default.
bool valid = false;
bool incase = false;
2011-10-13 20:53:06 +02:00
for (const Token * _tok = tok2->tokAt(2); _tok; _tok = _tok->next()) {
if (_tok->str() == "{")
break;
2011-10-13 20:53:06 +02:00
else if (_tok->str() == "}") {
valid = true;
break;
}
else if (_tok->str() == "switch")
break;
else if (_tok->str() == "loop")
break;
else if (incase && _tok->str() == "case")
break;
else if (Token::Match(_tok, "return !!;"))
break;
if (Token::Match(_tok, "if return|break use| ;"))
_tok = _tok->tokAt(2);
incase = incase || (_tok->str() == "case");
incase = incase && (_tok->str() != "break" && _tok->str() != "return");
}
2011-10-13 20:53:06 +02:00
if (!incase && valid) {
done = false;
tok2->str(";");
tok2->deleteNext();
tok2 = tok2->next();
bool first = true;
2011-10-13 20:53:06 +02:00
while (Token::Match(tok2, "case|default")) {
const bool def(tok2->str() == "default");
tok2->str(first ? "if" : "}");
2011-10-13 20:53:06 +02:00
if (first) {
first = false;
tok2->insertToken("{");
2011-10-13 20:53:06 +02:00
} else {
// Insert "else [if] {
tok2->insertToken("{");
if (! def)
tok2->insertToken("if");
tok2->insertToken("else");
tok2 = tok2->next();
}
2011-10-13 20:53:06 +02:00
while (tok2) {
if (tok2->str() == "}")
break;
if (Token::Match(tok2, "break|return ;"))
break;
if (Token::Match(tok2, "if return|break use| ;"))
tok2 = tok2->tokAt(2);
else
tok2 = tok2->next();
}
2011-10-13 20:53:06 +02:00
if (Token::simpleMatch(tok2, "break ;")) {
tok2->str(";");
tok2 = tok2->tokAt(2);
2011-10-13 20:53:06 +02:00
} else if (tok2 && tok2->str() == "return") {
tok2 = tok2->tokAt(2);
}
}
}
}
}
// If "--all" is given, remove all "callfunc"..
if (done && printExperimental) {
2011-10-13 20:53:06 +02:00
for (Token *tok2 = tok; tok2; tok2 = tok2->next()) {
if (tok2->str() == "callfunc") {
tok2->deleteThis();
done = false;
}
}
}
}
}
const Token *CheckMemoryLeakInFunction::findleak(const Token *tokens)
{
2009-10-08 22:30:33 +02:00
const Token *result;
2011-10-13 20:53:06 +02:00
if (Token::Match(tokens, "alloc ; if|if(var)|ifv break|continue|return ;")) {
return tokens->tokAt(3);
}
if ((result = Token::findsimplematch(tokens, "loop alloc ;")) != nullptr) {
return result;
}
2014-02-15 16:17:25 +01:00
if ((result = Token::findmatch(tokens, "alloc ; if|if(var)|ifv return ;")) != nullptr) {
return result->tokAt(3);
}
2014-02-15 16:17:25 +01:00
if ((result = Token::findmatch(tokens, "alloc ; alloc|assign|return callfunc| ;")) != nullptr) {
return result->tokAt(2);
}
if ((result = Token::findmatch(tokens, "alloc ; loop|while1 {| alloc ;")) != nullptr) {
return result->tokAt(3 + (result->strAt(3) == "{"));
}
2014-02-15 16:17:25 +01:00
if ((result = Token::findsimplematch(tokens, "; alloc ; if assign ;")) != nullptr) {
return result->tokAt(4);
2009-09-01 19:33:17 +02:00
}
if (((result = Token::findsimplematch(tokens, "; alloc ; if dealloc ; }")) != nullptr) ||
((result = Token::findsimplematch(tokens, "; alloc ; if dealloc ; return ;")) != nullptr)) {
return result->tokAt(6);
}
2014-02-15 16:17:25 +01:00
if ((result = Token::findsimplematch(tokens, "alloc ; }")) != nullptr) {
if (result->tokAt(3) == nullptr)
return result->tokAt(2);
}
// No deallocation / usage => report leak at the last token
2011-10-13 20:53:06 +02:00
if (!Token::findmatch(tokens, "dealloc|use")) {
const Token *last = tokens;
while (last->next())
last = last->next();
2010-12-15 18:45:53 +01:00
// not a leak if exit is called before the end of the function
if (!Token::Match(last->tokAt(-2), "exit|callfunc ; }"))
return last;
}
2014-02-15 16:17:25 +01:00
return nullptr;
}
// Check for memory leaks for a function variable.
2015-04-01 15:39:45 +02:00
void CheckMemoryLeakInFunction::checkScope(const Token *startTok, const std::string &varname, unsigned int varid, bool classmember, unsigned int sz)
{
2018-04-04 21:51:31 +02:00
const std::list<const Token *> callstack;
AllocType alloctype = No;
AllocType dealloctype = No;
const Token *result;
2015-04-01 15:39:45 +02:00
Token *tok = getcode(startTok, callstack, varid, alloctype, dealloctype, classmember, sz);
//tok->printOut((std::string("Checkmemoryleak: getcode result for: ") + varname).c_str());
2014-02-15 16:17:25 +01:00
const bool use_addr = bool(Token::findsimplematch(tok, "&use") != nullptr);
// Simplify the code and check if freed memory is used..
2011-10-13 20:53:06 +02:00
for (Token *tok2 = tok; tok2; tok2 = tok2->next()) {
while (Token::Match(tok2, "[;{}] ;"))
tok2->deleteNext();
}
2014-02-15 16:17:25 +01:00
if ((result = Token::findmatch(tok, "[;{}] dealloc ; use_ ;")) != nullptr) {
deallocuseError(result->tokAt(3), varname);
}
// Replace "&use" with "use". Replace "use_" with ";"
2011-10-13 20:53:06 +02:00
for (Token *tok2 = tok; tok2; tok2 = tok2->next()) {
if (tok2->str() == "&use")
tok2->str("use");
else if (tok2->str() == "use_")
tok2->str(";");
2010-11-27 11:09:42 +01:00
else if (Token::simpleMatch(tok2, "loop use_ {"))
tok2->deleteNext();
else if (tok2->str() == "::use") // Some kind of member function usage. Not analyzed very well.
tok2->str("use");
else if (tok2->str() == "recursive")
tok2->str("use");
else if (tok2->str() == "dealloc_")
tok2->str("dealloc");
2011-10-13 20:53:06 +02:00
else if (tok2->str() == "realloc") {
tok2->str("dealloc");
tok2->insertToken("alloc");
tok2->insertToken(";");
}
}
// If the variable is not allocated at all => no memory leak
if (Token::findsimplematch(tok, "alloc") == nullptr) {
TokenList::deleteTokens(tok);
return;
}
simplifycode(tok);
if (mSettings->debug && mSettings->verbose) {
tok->printOut(("Checkmemoryleak: simplifycode result for: " + varname).c_str());
}
// If the variable is not allocated at all => no memory leak
if (Token::findsimplematch(tok, "alloc") == nullptr) {
TokenList::deleteTokens(tok);
return;
}
/** @todo handle "goto" */
if (Token::findsimplematch(tok, "goto")) {
TokenList::deleteTokens(tok);
return;
}
2014-02-15 16:17:25 +01:00
if ((result = findleak(tok)) != nullptr) {
memoryLeak(result, varname, alloctype);
}
2014-02-15 16:17:25 +01:00
else if (!use_addr && (result = Token::findsimplematch(tok, "dealloc ; dealloc ;")) != nullptr) {
2009-03-21 17:58:13 +01:00
deallocDeallocError(result->tokAt(2), varname);
}
// detect cases that "simplifycode" don't handle well..
else if (tok && mSettings->debugwarnings) {
Token *first = tok;
while (first && first->str() == ";")
first = first->next();
bool noerr = false;
noerr = noerr || Token::simpleMatch(first, "alloc ; }");
noerr = noerr || Token::simpleMatch(first, "alloc ; dealloc ; }");
noerr = noerr || Token::simpleMatch(first, "alloc ; return use ; }");
noerr = noerr || Token::simpleMatch(first, "alloc ; use ; }");
noerr = noerr || Token::simpleMatch(first, "alloc ; use ; return ; }");
noerr = noerr || Token::simpleMatch(first, "alloc ; dealloc ; return ; }");
noerr = noerr || Token::simpleMatch(first, "if alloc ; dealloc ; }");
noerr = noerr || Token::simpleMatch(first, "if alloc ; return use ; }");
noerr = noerr || Token::simpleMatch(first, "if alloc ; use ; }");
noerr = noerr || Token::simpleMatch(first, "alloc ; ifv return ; dealloc ; }");
noerr = noerr || Token::simpleMatch(first, "alloc ; if return ; dealloc; }");
// Unhandled case..
if (!noerr)
reportError(first, Severity::debug, "debug",
"inconclusive leak of " + varname + ": " + tok->stringifyList(false, false, false, false, false, nullptr, nullptr));
}
TokenList::deleteTokens(tok);
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Check for memory leaks due to improper realloc() usage.
// Below, "a" may be set to null without being freed if realloc() cannot
// allocate the requested memory:
2010-05-18 20:08:27 +02:00
// a = malloc(10); a = realloc(a, 100);
//---------------------------------------------------------------------------
static bool isNoArgument(const SymbolDatabase* symbolDatabase, unsigned int varid)
{
const Variable* var = symbolDatabase->getVariableFromVarId(varid);
return var && !var->isArgument();
}
void CheckMemoryLeakInFunction::checkReallocUsage()
{
// only check functions
const std::size_t functions = symbolDatabase->functionScopes.size();
for (std::size_t i = 0; i < functions; ++i) {
const Scope * scope = symbolDatabase->functionScopes[i];
// Search for the "var = realloc(var, 100" pattern within this function
for (const Token *tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
if (tok->varId() > 0 &&
Token::Match(tok, "%name% = realloc|g_try_realloc ( %name% ,") &&
tok->varId() == tok->tokAt(4)->varId() &&
isNoArgument(symbolDatabase, tok->varId())) {
// Check that another copy of the pointer wasn't saved earlier in the function
if (Token::findmatch(scope->bodyStart, "%name% = %varid% ;", tok, tok->varId()) ||
Token::findmatch(scope->bodyStart, "[{};] %varid% = %name% [;=]", tok, tok->varId()))
2011-01-13 07:33:46 +01:00
continue;
const Token* tokEndRealloc = tok->linkAt(3);
2011-01-13 07:33:46 +01:00
// Check that the allocation isn't followed immediately by an 'if (!var) { error(); }' that might handle failure
2015-07-22 12:31:18 +02:00
if (Token::simpleMatch(tokEndRealloc->next(), "; if (") &&
notvar(tokEndRealloc->tokAt(3)->astOperand2(), tok->varId())) {
const Token* tokEndBrace = tokEndRealloc->linkAt(3)->linkAt(1);
if (tokEndBrace && mTokenizer->IsScopeNoReturn(tokEndBrace))
2011-01-13 07:33:46 +01:00
continue;
}
memleakUponReallocFailureError(tok, tok->str());
} else if (tok->next()->varId() > 0 &&
(Token::Match(tok, "* %name% = realloc|g_try_realloc ( * %name% ,") &&
tok->next()->varId() == tok->tokAt(6)->varId()) &&
isNoArgument(symbolDatabase, tok->next()->varId())) {
// Check that another copy of the pointer wasn't saved earlier in the function
if (Token::findmatch(scope->bodyStart, "%name% = * %varid% ;", tok, tok->next()->varId()) ||
Token::findmatch(scope->bodyStart, "[{};] * %varid% = %name% [;=]", tok, tok->next()->varId()))
continue;
const Token* tokEndRealloc = tok->linkAt(4);
// Check that the allocation isn't followed immediately by an 'if (!var) { error(); }' that might handle failure
if (Token::Match(tokEndRealloc->next(), "; if ( ! * %varid% ) {", tok->next()->varId())) {
const Token* tokEndBrace = tokEndRealloc->linkAt(8);
if (tokEndBrace && Token::simpleMatch(tokEndBrace->tokAt(-2), ") ;") &&
Token::Match(tokEndBrace->linkAt(-2)->tokAt(-2), "{|}|; %name% ("))
continue;
}
memleakUponReallocFailureError(tok->next(), tok->strAt(1));
}
}
}
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Checks for memory leaks inside function..
//---------------------------------------------------------------------------
static bool isInMemberFunc(const Scope* scope)
{
while (scope->nestedIn && !scope->functionOf)
scope = scope->nestedIn;
return (scope->functionOf != nullptr);
}
void CheckMemoryLeakInFunction::check()
{
// Check locking/unlocking of global resources..
for (const Scope * scope : symbolDatabase->functionScopes) {
if (!scope->hasInlineOrLambdaFunction())
checkScope(scope->bodyStart->next(), emptyString, 0, scope->functionOf != nullptr, 1);
}
// Check variables..
for (const Variable* var : symbolDatabase->variableList()) {
if (!var || (!var->isLocal() && !var->isArgument()) || var->isStatic() || !var->scope())
continue;
if (var->isReference())
continue;
if (!var->isPointer() && var->typeStartToken()->str() != "int")
continue;
// check for known class without implementation (forward declaration)
if (var->isPointer() && var->type() && !var->typeScope())
continue;
if (var->scope()->hasInlineOrLambdaFunction())
continue;
unsigned int sz = mTokenizer->sizeOfType(var->typeStartToken());
if (sz < 1)
sz = 1;
if (var->isArgument())
checkScope(var->scope()->bodyStart->next(), var->name(), var->declarationId(), isInMemberFunc(var->scope()), sz);
else
checkScope(var->nameToken(), var->name(), var->declarationId(), isInMemberFunc(var->scope()), sz);
}
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Checks for memory leaks in classes..
//---------------------------------------------------------------------------
void CheckMemoryLeakInClass::check()
{
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
// only check classes and structures
const std::size_t classes = symbolDatabase->classAndStructScopes.size();
for (std::size_t i = 0; i < classes; ++i) {
const Scope * scope = symbolDatabase->classAndStructScopes[i];
2018-04-05 21:41:31 +02:00
for (std::list<Variable>::const_iterator var = scope->varlist.begin(); var != scope->varlist.end(); ++var) {
if (!var->isStatic() && var->isPointer()) {
// allocation but no deallocation of private variables in public function..
const Token *tok = var->typeStartToken();
// Either it is of standard type or a non-derived type
if (tok->isStandardType() || (var->type() && var->type()->derivedFrom.empty())) {
if (var->isPrivate())
2015-01-17 16:28:39 +01:00
checkPublicFunctions(scope, var->nameToken());
2015-01-17 16:28:39 +01:00
variable(scope, var->nameToken());
}
}
}
}
}
void CheckMemoryLeakInClass::variable(const Scope *scope, const Token *tokVarname)
{
const std::string& varname = tokVarname->str();
const unsigned int varid = tokVarname->varId();
const std::string& classname = scope->className;
// Check if member variable has been allocated and deallocated..
CheckMemoryLeak::AllocType Alloc = CheckMemoryLeak::No;
CheckMemoryLeak::AllocType Dealloc = CheckMemoryLeak::No;
bool allocInConstructor = false;
bool deallocInDestructor = false;
// Inspect member functions
std::list<Function>::const_iterator func;
2011-10-13 20:53:06 +02:00
for (func = scope->functionList.begin(); func != scope->functionList.end(); ++func) {
const bool constructor = func->isConstructor();
const bool destructor = func->isDestructor();
if (!func->hasBody()) {
if (destructor) { // implementation for destructor is not seen => assume it deallocates all variables properly
deallocInDestructor = true;
Dealloc = CheckMemoryLeak::Many;
}
continue;
}
bool body = false;
const Token *end = func->functionScope->bodyEnd;
for (const Token *tok = func->arg->link(); tok != end; tok = tok->next()) {
if (tok == func->functionScope->bodyStart)
body = true;
else {
if (!body) {
if (!Token::Match(tok, ":|, %varid% (", varid))
continue;
}
// Allocate..
if (!body || Token::Match(tok, "%varid% =", varid)) {
// var1 = var2 = ...
// bail out
if (tok->strAt(-1) == "=")
return;
// Foo::var1 = ..
// bail out when not same class
if (tok->strAt(-1) == "::" &&
tok->strAt(-2) != scope->className)
return;
AllocType alloc = getAllocationType(tok->tokAt(body ? 2 : 3), 0);
2011-10-13 20:53:06 +02:00
if (alloc != CheckMemoryLeak::No) {
if (constructor)
allocInConstructor = true;
if (Alloc != No && Alloc != alloc)
alloc = CheckMemoryLeak::Many;
2011-10-13 20:53:06 +02:00
if (alloc != CheckMemoryLeak::Many && Dealloc != CheckMemoryLeak::No && Dealloc != CheckMemoryLeak::Many && Dealloc != alloc) {
2015-08-15 19:46:31 +02:00
std::list<const Token *> callstack;
callstack.push_back(tok);
mismatchAllocDealloc(callstack, classname + "::" + varname);
}
Alloc = alloc;
}
}
if (!body)
continue;
// Deallocate..
AllocType dealloc = getDeallocationType(tok, varid);
// some usage in the destructor => assume it's related
// to deallocation
if (destructor && tok->str() == varname)
dealloc = CheckMemoryLeak::Many;
2011-10-13 20:53:06 +02:00
if (dealloc != CheckMemoryLeak::No) {
if (destructor)
deallocInDestructor = true;
// several types of allocation/deallocation?
if (Dealloc != CheckMemoryLeak::No && Dealloc != dealloc)
dealloc = CheckMemoryLeak::Many;
2011-10-13 20:53:06 +02:00
if (dealloc != CheckMemoryLeak::Many && Alloc != CheckMemoryLeak::No && Alloc != Many && Alloc != dealloc) {
2015-08-15 19:46:31 +02:00
std::list<const Token *> callstack;
2009-02-01 16:47:36 +01:00
callstack.push_back(tok);
mismatchAllocDealloc(callstack, classname + "::" + varname);
2009-02-01 16:47:36 +01:00
}
Dealloc = dealloc;
}
// Function call .. possible deallocation
else if (Token::Match(tok->previous(), "[{};] %name% (")) {
if (!CheckMemoryLeakInFunction::test_white_list(tok->str(), mSettings, tokenizer->isCPP())) {
return;
}
}
}
}
}
2011-10-13 20:53:06 +02:00
if (allocInConstructor && !deallocInDestructor) {
unsafeClassError(tokVarname, classname, classname + "::" + varname /*, Alloc*/);
2011-10-13 20:53:06 +02:00
} else if (Alloc != CheckMemoryLeak::No && Dealloc == CheckMemoryLeak::No) {
unsafeClassError(tokVarname, classname, classname + "::" + varname /*, Alloc*/);
}
}
void CheckMemoryLeakInClass::unsafeClassError(const Token *tok, const std::string &classname, const std::string &varname)
{
if (!mSettings->isEnabled(Settings::STYLE))
return;
reportError(tok, Severity::style, "unsafeClassCanLeak",
"$symbol:" + classname + "\n"
"$symbol:" + varname + "\n"
"Class '" + classname + "' is unsafe, '" + varname + "' can leak by wrong usage.\n"
"The class '" + classname + "' is unsafe, wrong usage can cause memory/resource leaks for '" + varname + "'. This can for instance be fixed by adding proper cleanup in the destructor.", CWE398, false);
}
void CheckMemoryLeakInClass::checkPublicFunctions(const Scope *scope, const Token *classtok)
{
// Check that public functions deallocate the pointers that they allocate.
// There is no checking how these functions are used and therefore it
// isn't established if there is real leaks or not.
if (!mSettings->isEnabled(Settings::WARNING))
return;
const unsigned int varid = classtok->varId();
// Parse public functions..
// If they allocate member variables, they should also deallocate
std::list<Function>::const_iterator func;
2011-10-13 20:53:06 +02:00
for (func = scope->functionList.begin(); func != scope->functionList.end(); ++func) {
if ((func->type == Function::eFunction || func->type == Function::eOperatorEqual) &&
func->access == Public && func->hasBody()) {
const Token *tok2 = func->functionScope->bodyStart->next();
if (Token::Match(tok2, "%varid% =", varid)) {
const CheckMemoryLeak::AllocType alloc = getAllocationType(tok2->tokAt(2), varid);
if (alloc != CheckMemoryLeak::No)
publicAllocationError(tok2, tok2->str());
} else if (Token::Match(tok2, "%type% :: %varid% =", varid) &&
tok2->str() == scope->className) {
const CheckMemoryLeak::AllocType alloc = getAllocationType(tok2->tokAt(4), varid);
if (alloc != CheckMemoryLeak::No)
publicAllocationError(tok2, tok2->strAt(2));
}
}
}
}
void CheckMemoryLeakInClass::publicAllocationError(const Token *tok, const std::string &varname)
{
reportError(tok, Severity::warning, "publicAllocationError", "$symbol:" + varname + "\nPossible leak in public function. The pointer '$symbol' is not deallocated before it is allocated.", CWE398, false);
}
void CheckMemoryLeakStructMember::check()
{
const SymbolDatabase* symbolDatabase = mTokenizer->getSymbolDatabase();
for (const Variable* var : symbolDatabase->variableList()) {
if (!var || !var->isLocal() || var->isStatic())
continue;
if (var->typeEndToken()->isStandardType())
continue;
checkStructVariable(var);
}
}
bool CheckMemoryLeakStructMember::isMalloc(const Variable *variable)
{
const unsigned int declarationId(variable->declarationId());
bool alloc = false;
for (const Token *tok2 = variable->nameToken(); tok2 && tok2 != variable->scope()->bodyEnd; tok2 = tok2->next()) {
if (Token::Match(tok2, "= %varid% [;=]", declarationId)) {
return false;
} else if (Token::Match(tok2, "%varid% = malloc|kmalloc (", declarationId)) {
alloc = true;
}
}
return alloc;
}
void CheckMemoryLeakStructMember::checkStructVariable(const Variable * const variable)
{
// Is struct variable a pointer?
if (variable->isPointer()) {
// Check that variable is allocated with malloc
if (!isMalloc(variable))
return;
} else if (!mTokenizer->isC() && (!variable->typeScope() || variable->typeScope()->getDestructor())) {
2011-12-10 11:55:14 +01:00
// For non-C code a destructor might cleanup members
return;
}
// Check struct..
unsigned int indentlevel2 = 0;
for (const Token *tok2 = variable->nameToken(); tok2 && tok2 != variable->scope()->bodyEnd; tok2 = tok2->next()) {
if (tok2->str() == "{")
++indentlevel2;
2011-10-13 20:53:06 +02:00
else if (tok2->str() == "}") {
if (indentlevel2 == 0)
break;
--indentlevel2;
}
// Unknown usage of struct
/** @todo Check how the struct is used. Only bail out if necessary */
else if (Token::Match(tok2, "[(,] %varid% [,)]", variable->declarationId()))
break;
// Struct member is allocated => check if it is also properly deallocated..
else if (Token::Match(tok2->previous(), "[;{}] %varid% . %var% =", variable->declarationId())) {
if (getAllocationType(tok2->tokAt(4), tok2->tokAt(2)->varId()) == AllocType::No)
continue;
const unsigned int structid(variable->declarationId());
const unsigned int structmemberid(tok2->tokAt(2)->varId());
// This struct member is allocated.. check that it is deallocated
unsigned int indentlevel3 = indentlevel2;
2011-10-13 20:53:06 +02:00
for (const Token *tok3 = tok2; tok3; tok3 = tok3->next()) {
if (tok3->str() == "{")
++indentlevel3;
2011-10-13 20:53:06 +02:00
else if (tok3->str() == "}") {
if (indentlevel3 == 0) {
memoryLeak(tok3, variable->name() + "." + tok2->strAt(2), Malloc);
break;
}
--indentlevel3;
}
// Deallocating the struct member..
else if (getDeallocationType(tok3, structmemberid) != AllocType::No) {
// If the deallocation happens at the base level, don't check this member anymore
if (indentlevel3 == 0)
break;
// deallocating and then returning from function in a conditional block =>
// skip ahead out of the block
bool ret = false;
2011-10-13 20:53:06 +02:00
while (tok3) {
if (tok3->str() == "return")
ret = true;
else if (tok3->str() == "{" || tok3->str() == "}")
break;
tok3 = tok3->next();
}
if (!ret || !tok3 || tok3->str() != "}")
break;
--indentlevel3;
continue;
}
// Deallocating the struct..
else if (Token::Match(tok3, "free|kfree ( %varid% )", structid)) {
if (indentlevel2 == 0)
memoryLeak(tok3, variable->name() + "." + tok2->strAt(2), Malloc);
break;
}
// failed allocation => skip code..
else if (Token::simpleMatch(tok3, "if (") &&
notvar(tok3->next()->astOperand2(), structmemberid)) {
// Goto the ")"
tok3 = tok3->next()->link();
// make sure we have ") {".. it should be
if (!Token::simpleMatch(tok3, ") {"))
break;
// Goto the "}"
tok3 = tok3->next()->link();
}
// succeeded allocation
else if (ifvar(tok3, structmemberid, "!=", "0")) {
// goto the ")"
tok3 = tok3->next()->link();
// check if the variable is deallocated or returned..
unsigned int indentlevel4 = 0;
2011-10-13 20:53:06 +02:00
for (const Token *tok4 = tok3; tok4; tok4 = tok4->next()) {
if (tok4->str() == "{")
++indentlevel4;
2011-10-13 20:53:06 +02:00
else if (tok4->str() == "}") {
--indentlevel4;
if (indentlevel4 == 0)
break;
2011-10-13 20:53:06 +02:00
} else if (Token::Match(tok4, "free|kfree ( %var% . %varid% )", structmemberid)) {
break;
}
}
// was there a proper deallocation?
if (indentlevel4 > 0)
break;
}
// Returning from function..
2011-10-13 20:53:06 +02:00
else if (tok3->str() == "return") {
// Returning from function without deallocating struct member?
if (!Token::Match(tok3, "return %varid% ;", structid) &&
!Token::Match(tok3, "return & %varid%", structid) &&
!(Token::Match(tok3, "return %varid% . %var%", structid) && tok3->tokAt(3)->varId() == structmemberid)) {
memoryLeak(tok3, variable->name() + "." + tok2->strAt(2), Malloc);
}
break;
}
// struct assignment..
2011-10-13 20:53:06 +02:00
else if (Token::Match(tok3, "= %varid% ;", structid)) {
break;
} else if (Token::Match(tok3, "= %var% . %varid% ;", structmemberid)) {
break;
}
// goto isn't handled well.. bail out even though there might be leaks
else if (tok3->str() == "goto")
break;
// using struct in a function call..
else if (Token::Match(tok3, "%name% (")) {
// Calling non-function / function that doesn't deallocate?
if (CheckMemoryLeakInFunction::test_white_list(tok3->str(), mSettings, tokenizer->isCPP()))
continue;
// Check if the struct is used..
bool deallocated = false;
const Token* const end4 = tok3->linkAt(1);
for (const Token *tok4 = tok3; tok4 != end4; tok4 = tok4->next()) {
2011-10-13 20:53:06 +02:00
if (Token::Match(tok4, "[(,] &| %varid% [,)]", structid)) {
/** @todo check if the function deallocates the memory */
deallocated = true;
break;
}
if (Token::Match(tok4, "[(,] &| %varid% . %name% [,)]", structid)) {
/** @todo check if the function deallocates the memory */
deallocated = true;
break;
}
}
if (deallocated)
break;
}
}
}
}
}
2009-03-21 17:58:13 +01:00
void CheckMemoryLeakNoVar::check()
{
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
// only check functions
const std::size_t functions = symbolDatabase->functionScopes.size();
for (std::size_t i = 0; i < functions; ++i) {
const Scope * scope = symbolDatabase->functionScopes[i];
// Checks if a call to an allocation function like malloc() is made and its return value is not assigned.
checkForUnusedReturnValue(scope);
// Checks to see if a function is called with memory allocated for an argument that
// could be leaked if a function called for another argument throws.
checkForUnsafeArgAlloc(scope);
// parse the executable scope until tok is reached...
for (const Token *tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) {
// allocating memory in parameter for function call..
if (!(Token::Match(tok, "[(,] %name% (") && Token::Match(tok->linkAt(2), ") [,)]")))
continue;
if (getAllocationType(tok->next(), 0) == No)
continue;
// locate outer function call..
const Token* tok3 = tok;
while (tok3 && tok3->astParent() && tok3->str() == ",")
tok3 = tok3->astParent();
if (!tok3 || tok3->str() != "(")
continue;
// Is it a function call..
if (!Token::Match(tok3->tokAt(-2), "!!= %name% ("))
continue;
const std::string& functionName = tok3->strAt(-1);
if ((tokenizer->isCPP() && functionName == "delete") ||
functionName == "free" ||
functionName == "fclose" ||
functionName == "realloc")
break;
if (CheckMemoryLeakInFunction::test_white_list(functionName, mSettings, tokenizer->isCPP())) {
functionCallLeak(tok, tok->strAt(1), functionName);
break;
}
}
}
}
//---------------------------------------------------------------------------
// Checks if a call to an allocation function like malloc() is made and its return value is not assigned.
//---------------------------------------------------------------------------
void CheckMemoryLeakNoVar::checkForUnusedReturnValue(const Scope *scope)
{
for (const Token *tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) {
if (!Token::Match(tok, "%name% ("))
continue;
if (tok->varId())
continue;
const AllocType allocType = getAllocationType(tok, 0);
if (allocType == No)
continue;
if (tok != tok->next()->astOperand1())
continue;
// get ast parent, skip casts
const Token *parent = tok->next()->astParent();
while (parent && parent->str() == "(" && !parent->astOperand2())
parent = parent->astParent();
if (!parent) {
// Check if we are in a C++11 constructor
const Token * closingBrace = Token::findmatch(tok, "}|;");
2018-04-05 08:21:43 +02:00
if (closingBrace->str() == "}" && Token::Match(closingBrace->link()->tokAt(-1), "%name%"))
continue;
returnValueNotUsedError(tok, tok->str());
} else if (Token::Match(parent, "%comp%|!")) {
returnValueNotUsedError(tok, tok->str());
}
}
}
//---------------------------------------------------------------------------
// Check if an exception could cause a leak in an argument constructed with
// shared_ptr/unique_ptr. For example, in the following code, it is possible
// that if g() throws an exception, the memory allocated by "new int(42)"
// could be leaked. See stackoverflow.com/questions/19034538/
// why-is-there-memory-leak-while-using-shared-ptr-as-a-function-parameter
//
// void x() {
// f(shared_ptr<int>(new int(42)), g());
// }
//---------------------------------------------------------------------------
void CheckMemoryLeakNoVar::checkForUnsafeArgAlloc(const Scope *scope)
{
// This test only applies to C++ source
if (!mTokenizer->isCPP() || !mSettings->inconclusive || !mSettings->isEnabled(Settings::WARNING))
return;
for (const Token *tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) {
if (Token::Match(tok, "%name% (")) {
const Token *endParamToken = tok->next()->link();
const Token* pointerType = nullptr;
const Token* functionCalled = nullptr;
// Scan through the arguments to the function call
for (const Token *tok2 = tok->tokAt(2); tok2 && tok2 != endParamToken; tok2 = tok2->nextArgument()) {
2015-02-27 10:02:12 +01:00
const Function *func = tok2->function();
const bool isNothrow = func && (func->isAttributeNothrow() || func->isThrow());
if (Token::Match(tok2, "shared_ptr|unique_ptr <") && tok2->next()->link() && Token::Match(tok2->next()->link(), "> ( new %name%")) {
pointerType = tok2;
} else if (!isNothrow) {
if (Token::Match(tok2, "%name% ("))
functionCalled = tok2;
else if (tok2->isName() && tok2->next()->link() && Token::simpleMatch(tok2->next()->link(), "> ("))
functionCalled = tok2;
}
}
if (pointerType && functionCalled) {
std::string functionName = functionCalled->str();
if (functionCalled->strAt(1) == "<") {
functionName += '<';
for (const Token* tok2 = functionCalled->tokAt(2); tok2 != functionCalled->next()->link(); tok2 = tok2->next())
functionName += tok2->str();
functionName += '>';
}
std::string objectTypeName;
for (const Token* tok2 = pointerType->tokAt(2); tok2 != pointerType->next()->link(); tok2 = tok2->next())
objectTypeName += tok2->str();
unsafeArgAllocError(tok, functionName, pointerType->str(), objectTypeName);
}
}
}
}
void CheckMemoryLeakNoVar::functionCallLeak(const Token *loc, const std::string &alloc, const std::string &functionCall)
{
reportError(loc, Severity::error, "leakNoVarFunctionCall", "Allocation with " + alloc + ", " + functionCall + " doesn't release it.", CWE772, false);
}
void CheckMemoryLeakNoVar::returnValueNotUsedError(const Token *tok, const std::string &alloc)
{
reportError(tok, Severity::error, "leakReturnValNotUsed", "$symbol:" + alloc + "\nReturn value of allocation function '$symbol' is not stored.", CWE771, false);
}
void CheckMemoryLeakNoVar::unsafeArgAllocError(const Token *tok, const std::string &funcName, const std::string &ptrType, const std::string& objType)
{
const std::string factoryFunc = ptrType == "shared_ptr" ? "make_shared" : "make_unique";
reportError(tok, Severity::warning, "leakUnsafeArgAlloc",
"$symbol:" + funcName + "\n"
"Unsafe allocation. If $symbol() throws, memory could be leaked. Use " + factoryFunc + "<" + objType + ">() instead.",
CWE401,
true); // Inconclusive because funcName may never throw
}