cppcheck/lib/checkmemoryleak.cpp

2909 lines
105 KiB
C++
Raw Normal View History

/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2011 Daniel Marjamäki and 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"
#include "mathlib.h"
2009-03-20 18:16:21 +01:00
#include "tokenize.h"
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <sstream>
#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;
}
2011-09-08 18:23:25 +02:00
/** 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 needs to be alphabetically sorted so we can run bsearch on it.
* This list contains function names with const parameters e.g.: atof(const char *)
2011-09-08 18:23:25 +02:00
* Reference: http://www.aquaphoenix.com/ref/gnu_c_library/libc_492.html#SEC492
*/
2011-10-13 20:53:06 +02:00
static const char * const call_func_white_list[] = {
2011-03-22 22:13:29 +01:00
"_open", "_wopen", "access", "adjtime", "asctime", "asctime_r", "asprintf", "assert"
, "atof", "atoi", "atol", "chdir", "chmod", "chown"
2011-03-22 22:13:29 +01:00
, "clearerr", "creat", "ctime", "ctime_r", "delete", "execl", "execle"
, "execlp", "execv", "execve", "fchmod", "fclose", "fcntl"
, "fdatasync", "feof", "ferror", "fflush", "fgetc", "fgetpos", "fgets"
2011-03-22 22:13:29 +01:00
, "flock", "fmemopen", "fnmatch", "fopen", "fopencookie", "for", "fprintf", "fputc", "fputs", "fread", "free"
, "freopen", "fscanf", "fseek", "fseeko", "fsetpos", "fstat", "fsync", "ftell", "ftello"
, "ftruncate", "fwrite", "getc", "getenv","getgrnam", "gethostbyaddr", "gethostbyname", "getnetbyname"
, "getopt", "getopt_long", "getprotobyname", "getpwnam", "gets", "getservbyname", "getservbyport"
, "glob", "gmtime", "gmtime_r", "if", "index", "inet_addr", "inet_aton", "inet_network", "initgroups", "ioctl"
, "link", "localtime", "localtime_r"
, "lockf", "lseek", "lstat", "mblen", "mbstowcs", "mbtowc", "memchr", "memcmp", "memcpy", "memmove", "memset"
, "mkdir", "mkfifo", "mknod", "mkstemp"
2011-03-22 22:13:29 +01:00
, "obstack_printf", "obstack_vprintf", "open", "opendir", "parse_printf_format", "pathconf"
, "perror", "popen" ,"posix_fadvise", "posix_fallocate", "pread"
, "printf", "psignal", "putenv", "puts", "pwrite", "qsort", "read", "readahead", "readdir", "readdir_r"
, "readlink", "readv"
2011-03-23 00:20:05 +01:00
, "realloc", "regcomp", "remove", "rename", "return", "rewind", "rewinddir", "rindex"
2011-03-22 22:13:29 +01:00
, "rmdir" ,"scandir", "scanf", "seekdir"
, "setbuf", "setbuffer", "sethostname", "setlinebuf", "setlocale" ,"setvbuf", "sizeof" ,"snprintf", "sprintf", "sscanf"
2011-03-21 23:59:43 +01:00
, "stat", "stpcpy", "strcasecmp", "strcat", "strchr", "strcmp", "strcoll"
, "strcpy", "strcspn", "strdup", "stricmp", "strlen", "strncasecmp", "strncat", "strncmp"
, "strncpy", "strpbrk","strrchr", "strspn", "strstr", "strtod", "strtok", "strtol", "strtoul", "strxfrm", "switch"
2011-03-22 22:13:29 +01:00
, "symlink", "sync_file_range", "system", "telldir", "tempnam", "time", "typeid", "unlink"
, "utime", "utimes", "vasprintf", "vfprintf", "vfscanf", "vprintf"
, "vscanf", "vsnprintf", "vsprintf", "vsscanf", "while", "wordexp","write", "writev"
};
static int call_func_white_list_compare(const void *a, const void *b)
{
return strcmp((const char *)a, *(const char **)b);
}
2009-03-20 18:16:21 +01:00
//---------------------------------------------------------------------------
bool CheckMemoryLeak::isclass(const Tokenizer *_tokenizer, 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->type() && var->type()->numConstructors == 0 &&
(var->type()->varlist.empty() || var->type()->needInitialization == Scope::True) &&
var->type()->derivedFrom.empty())
return false;
return true;
}
//---------------------------------------------------------------------------
CheckMemoryLeak::AllocType CheckMemoryLeak::getAllocationType(const Token *tok2, unsigned int varid, std::list<const Token *> *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();
tok2 = tok2 ? tok2->next() : NULL;
}
if (! tok2)
return No;
if (! tok2->isName())
return No;
// Does tok2 point on "malloc", "strdup" or "kmalloc"..
static const char * const mallocfunc[] = {"malloc",
"calloc",
"strdup",
"strndup",
"kmalloc",
"kzalloc",
"kcalloc",
0
};
2011-10-13 20:53:06 +02:00
for (unsigned int i = 0; mallocfunc[i]; i++) {
if (tok2->str() == mallocfunc[i])
return Malloc;
}
// Using realloc..
if (varid && Token::Match(tok2, "realloc ( %any% ,") && tok2->tokAt(2)->varId() != varid)
return Malloc;
// Does tok2 point on "g_malloc", "g_strdup", ..
static const char * const gmallocfunc[] = {"g_new",
"g_new0",
"g_try_new",
"g_try_new0",
"g_malloc",
"g_malloc0",
"g_try_malloc",
"g_try_malloc0",
"g_strdup",
"g_strndup",
"g_strdup_printf",
0
};
2011-10-13 20:53:06 +02:00
for (unsigned int i = 0; gmallocfunc[i]; i++) {
if (tok2->str() == gmallocfunc[i])
return gMalloc;
}
2011-08-07 01:10:15 +02:00
if (Token::Match(tok2, "new struct| %type% [;()]") ||
Token::Match(tok2, "new ( std :: nothrow ) struct| %type% [;()]") ||
Token::Match(tok2, "new ( nothrow ) struct| %type% [;()]"))
return New;
2011-08-07 01:10:15 +02:00
if (Token::Match(tok2, "new struct| %type% [") ||
Token::Match(tok2, "new ( std :: nothrow ) struct| %type% [") ||
Token::Match(tok2, "new ( nothrow ) struct| %type% ["))
return NewArray;
if (Token::Match(tok2, "fopen|tmpfile|g_fopen ("))
return File;
2011-10-13 20:53:06 +02:00
if (Token::Match(tok2, "open|openat|creat|mkstemp|mkostemp (")) {
// is there a user function with this name?
if (tokenizer && Token::findmatch(tokenizer->tokens(), ("%type% *|&| " + tok2->str()).c_str()))
return No;
return Fd;
}
if (Token::simpleMatch(tok2, "popen ("))
return Pipe;
if (Token::Match(tok2, "opendir|fdopendir ("))
return Dir;
// User function
const Token *ftok = tokenizer->getFunctionTokenByName(tok2->str().c_str());
if (ftok == NULL)
return No;
// Prevent recursion
if (callstack && std::find(callstack->begin(), callstack->end(), ftok) != callstack->end())
return No;
std::list<const Token *> cs;
if (!callstack)
callstack = &cs;
callstack->push_back(ftok);
return functionReturnType(ftok, callstack);
}
CheckMemoryLeak::AllocType CheckMemoryLeak::getReallocationType(const Token *tok2, unsigned int varid) const
{
// What we may have...
// * var = (char *)realloc(..;
2011-10-13 20:53:06 +02:00
if (tok2 && tok2->str() == "(") {
tok2 = tok2->link();
tok2 = tok2 ? tok2->next() : NULL;
}
if (! tok2)
return No;
if (varid > 0 && ! Token::Match(tok2, "%var% ( %varid% [,)]", varid))
return No;
if (tok2->str() == "realloc")
return Malloc;
// GTK memory reallocation..
if (Token::Match(tok2, "g_realloc|g_try_realloc|g_renew|g_try_renew"))
return gMalloc;
return No;
}
CheckMemoryLeak::AllocType CheckMemoryLeak::getDeallocationType(const Token *tok, unsigned int varid) const
{
if (Token::Match(tok, "delete %varid% ;", varid))
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 (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, "g_free ( %varid% ) ;", varid) ||
Token::Match(tok, "g_free ( %varid% -", varid))
return gMalloc;
if (Token::Match(tok, "fclose ( %varid% )", varid) ||
Token::simpleMatch(tok, "fcloseall ( )"))
return File;
if (Token::Match(tok, "close ( %varid% )", varid))
return Fd;
if (Token::Match(tok, "pclose ( %varid% )", varid))
return Pipe;
if (Token::Match(tok, "closedir ( %varid% )", varid))
return Dir;
return No;
}
CheckMemoryLeak::AllocType CheckMemoryLeak::getDeallocationType(const Token *tok, const std::string &varname) const
{
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("g_free ( " + varname + " ) ;").c_str()))
return gMalloc;
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::simpleMatch(tok, std::string("closedir ( " + varname + " )").c_str()))
return Dir;
return No;
}
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
void CheckMemoryLeak::memoryLeak(const Token *tok, const std::string &varname, AllocType alloctype)
{
if (alloctype == CheckMemoryLeak::File ||
alloctype == CheckMemoryLeak::Pipe ||
alloctype == CheckMemoryLeak::Fd ||
alloctype == CheckMemoryLeak::Dir)
2011-12-08 21:28:34 +01:00
resourceLeakError(tok, varname);
else
2011-12-08 21:28:34 +01:00
memleakError(tok, varname);
}
//---------------------------------------------------------------------------
void CheckMemoryLeak::reportErr(const Token *tok, Severity::SeverityType severity, const std::string &id, const std::string &msg) 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);
2009-08-04 21:36:55 +02:00
reportErr(callstack, severity, id, msg);
}
void CheckMemoryLeak::reportErr(const std::list<const Token *> &callstack, Severity::SeverityType severity, const std::string &id, const std::string &msg) const
{
std::list<ErrorLogger::ErrorMessage::FileLocation> locations;
2011-10-13 20:53:06 +02:00
for (std::list<const Token *>::const_iterator it = callstack.begin(); it != callstack.end(); ++it) {
2009-12-22 21:56:00 +01:00
const Token * const tok = *it;
ErrorLogger::ErrorMessage::FileLocation loc;
2009-12-22 21:56:00 +01:00
loc.line = tok->linenr();
loc.setfile(tokenizer->file(tok));
locations.push_back(loc);
}
const ErrorLogger::ErrorMessage errmsg(locations, severity, msg, id, false);
2009-08-04 21:36:55 +02:00
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)
{
reportErr(tok, Severity::error, "memleak", "Memory leak: " + varname);
}
void CheckMemoryLeak::memleakUponReallocFailureError(const Token *tok, const std::string &varname)
{
reportErr(tok, Severity::error, "memleakOnRealloc", "Common realloc mistake: \'" + varname + "\' nulled but not freed upon failure");
}
void CheckMemoryLeak::resourceLeakError(const Token *tok, const std::string &varname)
{
std::string errmsg("Resource leak");
if (!varname.empty())
errmsg += ": " + varname;
reportErr(tok, Severity::error, "resourceLeak", errmsg);
}
void CheckMemoryLeak::deallocDeallocError(const Token *tok, const std::string &varname)
{
2009-08-04 21:36:55 +02:00
reportErr(tok, Severity::error, "deallocDealloc", "Deallocating a deallocated pointer: " + varname);
}
void CheckMemoryLeak::deallocuseError(const Token *tok, const std::string &varname)
{
2009-08-04 21:36:55 +02:00
reportErr(tok, Severity::error, "deallocuse", "Dereferencing '" + varname + "' after it is deallocated / released");
}
void CheckMemoryLeak::mismatchSizeError(const Token *tok, const std::string &sz)
{
2009-08-04 21:36:55 +02:00
reportErr(tok, Severity::error, "mismatchSize", "The given size " + sz + " is mismatching");
}
void CheckMemoryLeak::mismatchAllocDealloc(const std::list<const Token *> &callstack, const std::string &varname)
{
reportErr(callstack, Severity::error, "mismatchAllocDealloc", "Mismatching allocation and deallocation: " + varname);
}
CheckMemoryLeak::AllocType CheckMemoryLeak::functionReturnType(const Token *tok, std::list<const Token *> *callstack) const
{
if (!tok)
return No;
// Locate start of function
2011-10-13 20:53:06 +02:00
while (tok) {
if (tok->str() == "{" || tok->str() == "}")
return No;
2011-10-13 20:53:06 +02:00
if (tok->str() == "(") {
tok = tok->link();
break;
}
tok = tok->next();
}
// Is this the start of a function?
if (!Token::Match(tok, ") const| {"))
return No;
while (tok->str() != "{")
tok = tok->next();
// Get return pointer..
unsigned int varid = 0;
unsigned int indentlevel = 0;
2011-10-13 20:53:06 +02:00
for (const Token *tok2 = tok; tok2; tok2 = tok2->next()) {
if (tok2->str() == "{")
++indentlevel;
2011-10-13 20:53:06 +02:00
else if (tok2->str() == "}") {
if (indentlevel <= 1)
return No;
--indentlevel;
}
2011-10-13 20:53:06 +02:00
if (Token::Match(tok2, "return %var% ;")) {
if (indentlevel != 1)
return No;
varid = tok2->next()->varId();
break;
2011-10-13 20:53:06 +02:00
} else if (tok2->str() == "return") {
AllocType allocType = getAllocationType(tok2->next(), 0, callstack);
if (allocType != No)
return allocType;
}
}
// Not returning pointer value..
if (varid == 0)
return No;
// Check if return pointer is allocated..
AllocType allocType = No;
while (NULL != (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;
}
2011-10-13 20:53:06 +02:00
if (Token::Match(tok, "static %type% * %varid% [;{}=]", varid)) {
return No;
}
2011-10-13 20:53:06 +02:00
if (Token::Match(tok, "[(,] %varid% [,)]", varid)) {
return No;
}
if (tok->str() == "return")
return allocType;
}
return allocType;
}
const char *CheckMemoryLeak::functionArgAlloc(const Token *tok, unsigned int targetpar, AllocType &allocType) const
{
// Find the varid of targetpar, then locate the start of the function..
unsigned int parlevel = 0;
unsigned int par = 0;
unsigned int varid = 0;
allocType = No;
2011-10-13 20:53:06 +02:00
while (tok) {
if (tok->str() == "{" || tok->str() == "}")
return "";
2011-10-13 20:53:06 +02:00
if (tok->str() == "(") {
if (parlevel != 0)
return "";
++parlevel;
++par;
}
2011-10-13 20:53:06 +02:00
else if (tok->str() == ")") {
if (parlevel != 1)
return "";
break;
}
2011-10-13 20:53:06 +02:00
else if (parlevel == 1 && tok->str() == ",") {
++par;
}
tok = tok->next();
2011-10-13 20:53:06 +02:00
if (parlevel == 1 && par == targetpar && Token::Match(tok, "%type% * * %var%")) {
varid = tok->tokAt(3)->varId();
}
}
if (varid == 0)
return "";
// Is this the start of a function?
if (!Token::Match(tok, ") const| {"))
return "";
while (tok->str() != "{")
tok = tok->next();
// Check if pointer is allocated.
unsigned int indentlevel = 0;
int realloc = 0;
while (NULL != (tok = tok->next())) {
if (tok->str() == "{")
++indentlevel;
2011-10-13 20:53:06 +02:00
else if (tok->str() == "}") {
if (indentlevel <= 1)
break;
--indentlevel;
2011-10-13 20:53:06 +02:00
} else if (tok->varId() == varid) {
if (Token::Match(tok->tokAt(-3), "free ( * %varid% )", varid)) {
realloc = 1;
allocType = No;
2011-10-13 20:53:06 +02:00
} else if (Token::Match(tok->previous(), "* %varid% =", varid)) {
allocType = getAllocationType(tok->tokAt(2), varid);
2011-10-13 20:53:06 +02:00
if (allocType == No) {
allocType = getReallocationType(tok->tokAt(2), varid);
}
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 "";
}
void CheckMemoryLeakInFunction::parse_noreturn()
{
2009-11-15 09:53:38 +01:00
noreturn.insert("exit");
noreturn.insert("_exit");
noreturn.insert("_Exit");
noreturn.insert("abort");
noreturn.insert("err");
noreturn.insert("verr");
noreturn.insert("errx");
noreturn.insert("verrx");
noreturn.insert("ExitProcess");
noreturn.insert("ExitThread");
noreturn.insert("pthread_exit");
2009-11-15 09:53:38 +01:00
std::list<Scope>::const_iterator scope;
2011-10-13 20:53:06 +02:00
for (scope = symbolDatabase->scopeList.begin(); scope != symbolDatabase->scopeList.end(); ++scope) {
// only check functions
if (scope->type != Scope::eFunction)
continue;
// parse this function to check if it contains an "exit" call..
unsigned int indentlevel = 1;
2011-10-13 20:53:06 +02:00
for (const Token *tok2 = scope->classStart->next(); tok2; tok2 = tok2->next()) {
if (tok2->str() == "{")
++indentlevel;
2011-10-13 20:53:06 +02:00
else if (tok2->str() == "}") {
--indentlevel;
if (indentlevel == 0)
break;
}
2011-10-13 20:53:06 +02:00
if (Token::Match(tok2->previous(), "[;{}] exit (")) {
noreturn.insert(scope->className);
break;
}
}
// This function is not a noreturn function
2011-10-13 20:53:06 +02:00
if (indentlevel == 0) {
notnoreturn.insert(scope->className);
}
}
}
bool CheckMemoryLeakInFunction::notvar(const Token *tok, unsigned int varid, bool endpar) const
{
const std::string end(endpar ? " &&|)" : " [;)&|]");
return bool(Token::Match(tok, ("! %varid%" + end).c_str(), varid) ||
Token::Match(tok, ("! ( %varid% )" + end).c_str(), varid));
}
2011-11-26 21:02:04 +01:00
static unsigned int countParameters(const Token *tok)
{
2011-11-26 21:02:04 +01:00
tok = tok->tokAt(2);
if (tok->str() == ")")
return 0;
2011-11-26 21:02:04 +01:00
unsigned int numpar = 1;
while (NULL != (tok = tok->nextArgument()))
2011-11-26 21:02:04 +01:00
numpar++;
2011-11-26 21:02:04 +01:00
return numpar;
}
bool CheckMemoryLeakInFunction::test_white_list(const std::string &funcname)
{
return (std::bsearch(funcname.c_str(), call_func_white_list,
sizeof(call_func_white_list) / sizeof(call_func_white_list[0]),
sizeof(call_func_white_list[0]), call_func_white_list_compare) != NULL);
}
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)
{
2011-10-13 20:53:06 +02:00
if (test_white_list(tok->str())) {
if (tok->str() == "asprintf" ||
tok->str() == "delete" ||
tok->str() == "fclose" ||
tok->str() == "for" ||
tok->str() == "free" ||
tok->str() == "if" ||
tok->str() == "realloc" ||
tok->str() == "return" ||
tok->str() == "switch" ||
2011-10-13 20:53:06 +02:00
tok->str() == "while") {
return 0;
}
// is the varid a parameter?
2011-10-13 20:53:06 +02:00
for (const Token *tok2 = tok->tokAt(2); tok2; tok2 = tok2->next()) {
if (tok2->str() == "(" || tok2->str() == ")")
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
return"use_";
}
}
return 0;
}
if (noreturn.find(tok->str()) != noreturn.end() && tok->strAt(-1) != "=")
return "exit";
if (varid > 0 && (getAllocationType(tok, varid) != No || getReallocationType(tok, varid) != No || getDeallocationType(tok, varid) != No))
return 0;
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 Token *ftok = _tokenizer->getFunctionTokenByName(funcname.c_str());
while (ftok && (ftok->str() != "{"))
ftok = ftok->next();
if (!ftok)
return 0;
Token *func = getcode(ftok->next(), callstack, 0, alloctype, dealloctype, false, 1);
simplifycode(func);
const char *ret = 0;
if (Token::simpleMatch(func, "; alloc ; }"))
ret = "alloc";
else if (Token::simpleMatch(func, "; dealloc ; }"))
ret = "dealloc";
Tokenizer::deleteTokens(func);
return ret;
}
// how many parameters is there in the function call?
2011-11-26 21:02:04 +01:00
unsigned int numpar = countParameters(tok);
if (numpar == 0) {
// Taking return value => it is not a noreturn function
if (tok->strAt(-1) == "=")
return NULL;
// Function is not noreturn
if (notnoreturn.find(funcname) != notnoreturn.end())
return NULL;
return (tok->previous()->str() != "=") ? "callfunc" : NULL;
}
unsigned int par = 1;
unsigned int parlevel = 0;
const bool dot(tok->previous()->str() == ".");
const bool eq(tok->previous()->str() == "=");
2011-10-13 20:53:06 +02:00
for (; tok; tok = tok->next()) {
if (tok->str() == "(")
++parlevel;
2011-10-13 20:53:06 +02:00
else if (tok->str() == ")") {
--parlevel;
2011-10-13 20:53:06 +02:00
if (parlevel < 1) {
return (eq || _settings->experimental) ? 0 : "callfunc";
}
}
2011-10-13 20:53:06 +02:00
if (parlevel == 1) {
if (tok->str() == ",")
++par;
2011-10-13 20:53:06 +02:00
if (varid > 0 && Token::Match(tok, "[,()] %varid% [,()]", varid)) {
if (dot)
return "use";
const Token *ftok = _tokenizer->getFunctionTokenByName(funcname.c_str());
if (!ftok)
2009-06-23 20:37:34 +02:00
return "use";
// how many parameters does the function want?
if (numpar != countParameters(ftok))
return "recursive";
const char *parname = Tokenizer::getParameterName(ftok, par);
if (! parname)
return "recursive";
unsigned int parameterVarid = 0;
{
const Token *partok = Token::findmatch(ftok, parname);
if (partok)
parameterVarid = partok->varId();
}
if (parameterVarid == 0)
return "recursive";
// Check if the function deallocates the variable..
while (ftok && (ftok->str() != "{"))
ftok = ftok->next();
if (!ftok)
return 0;
Token *func = getcode(ftok->next(), callstack, parameterVarid, alloctype, dealloctype, false, sz);
//simplifycode(func, all);
const Token *func_ = func;
while (func_ && func_->str() == ";")
func_ = func_->next();
const char *ret = 0;
/** @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";
Tokenizer::deleteTokens(func);
return ret;
}
2011-10-13 20:53:06 +02:00
if (varid > 0 && Token::Match(tok, "[,()] & %varid% [,()]", varid)) {
const Token *ftok = _tokenizer->getFunctionTokenByName(funcname.c_str());
AllocType a;
const char *ret = functionArgAlloc(ftok, par, a);
2011-10-13 20:53:06 +02:00
if (a != No) {
if (alloctype == No)
alloctype = a;
else if (alloctype != a)
alloctype = Many;
allocpar = true;
return ret;
}
}
if (varid > 0 && Token::Match(tok, "[(,] %varid% . %var% [,)]", varid))
return "use";
}
}
return NULL;
}
2009-02-04 07:11:36 +01:00
2010-09-04 10:06:34 +02:00
static void addtoken(Token **rettail, const Token *tok, const std::string &str)
{
(*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)
{
Token *rethead = 0, *rettail = 0;
// 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 ";"
2010-09-04 10:06:34 +02:00
rethead = new Token(0);
rethead->str(";");
rethead->linenr(tok->linenr());
rethead->fileIndex(tok->fileIndex());
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-10-13 20:53:06 +02:00
if (Token::Match(tok, "[;{}]") && varid > 0 && parlevel == 0) {
2011-01-16 09:56:04 +01:00
if (Token::Match(tok->next(), "[{};]"))
continue;
// function calls are interesting..
const Token *tok2 = tok;
while (Token::Match(tok2->next(), "%var% ."))
tok2 = tok2->tokAt(2);
if (Token::Match(tok2->next(), "%var% ("))
;
else if (Token::Match(tok->next(), "continue|break|return|throw|goto|do|else"))
;
2011-10-13 20:53:06 +02:00
else {
2011-01-16 09:56:04 +01:00
const Token *skipToToken = 0;
// 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) ||
2011-10-13 20:53:06 +02:00
Token::Match(tok, "asprintf ( & %varid% ,", varid)) {
CheckMemoryLeak::AllocType alloc;
2011-10-13 20:53:06 +02:00
if (Token::simpleMatch(tok, "asprintf (")) {
// todo: check how the return value is used.
2011-10-13 20:53:06 +02:00
if (!Token::Match(tok->previous(), "[;{}]")) {
Tokenizer::deleteTokens(rethead);
return 0;
}
alloc = Malloc;
tok = tok->next()->link();
2011-10-13 20:53:06 +02:00
} else {
alloc = getAllocationType(tok->tokAt(2), varid);
}
bool realloc = false;
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, ";");
realloc = true;
tok = tok->tokAt(2);
2011-01-16 09:56:04 +01:00
if (Token::Match(tok, "%var% ("))
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;
2011-10-13 20:53:06 +02:00
if (isclass(_tokenizer, 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;
2011-10-13 20:53:06 +02:00
if (isclass(_tokenizer, 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;
2011-10-13 20:53:06 +02:00
if (isclass(_tokenizer, tok->tokAt(8 + offset), varid)) {
alloc = No;
}
}
if (alloc == No && alloctype == No)
alloctype = CheckMemoryLeak::New;
}
2011-10-13 20:53:06 +02:00
if (alloc != No) {
if (! realloc)
2010-09-04 10:06:34 +02:00
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(_tokenizer->tokens(), "%varid%", varid)->str());
callstack.pop_back();
}
alloctype = alloc;
2011-10-13 20:53:06 +02:00
if (Token::Match(tok, "%var% = %type% (")) {
tok = tok->linkAt(3);
continue;
}
}
// assignment..
2011-10-13 20:53:06 +02:00
else {
// is the pointer in rhs?
bool rhs = false;
2011-10-13 20:53:06 +02:00
for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next()) {
if (tok2->str() == ";") {
if (rhs)
tok = tok2;
break;
}
2011-10-13 20:53:06 +02:00
if (Token::Match(tok2, "[=+(,] %varid%", varid)) {
if (!rhs && Token::Match(tok2, "[(,]")) {
addtoken(&rettail, tok, "use");
addtoken(&rettail, tok, ";");
}
rhs = true;
}
}
if (!rhs)
addtoken(&rettail, tok, "assign");
continue;
}
}
2011-10-13 20:53:06 +02:00
if (Token::Match(tok->previous(), "[;{})=|] %var%")) {
AllocType dealloc = getDeallocationType(tok, varid);
if (dealloc != No && tok->str() == "fcloseall" && alloctype != dealloc)
dealloc = No;
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(_tokenizer->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 (Token::Match(tok, "if ( 0 <=|< %varid% )", varid) ||
2011-10-13 20:53:06 +02:00
Token::Match(tok, "if ( %varid% != -1 )", varid)) {
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, "if(var)");
tok = tok->next()->link();
continue;
2011-10-13 20:53:06 +02:00
} else if (Token::Match(tok, "if ( %varid% == -1 )", varid) ||
Token::Match(tok, "if ( %varid% < 0 )", varid)) {
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
}
2011-10-13 20:53:06 +02:00
if (Token::Match(tok, "if ( %varid% )", varid)) {
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;
2011-10-13 20:53:06 +02:00
} else if (Token::simpleMatch(tok, "if (") && notvar(tok->tokAt(2), varid, true)) {
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;
2010-04-06 22:17:23 +02:00
int innerParlevel = 0;
2011-10-13 20:53:06 +02:00
for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next()) {
if (tok2->str() == "(")
2010-04-06 22:17:23 +02:00
++innerParlevel;
else if (tok2->str() == ")") {
2010-04-06 22:17:23 +02:00
--innerParlevel;
if (innerParlevel <= 0)
break;
} else 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 (innerParlevel > 0 && Token::Match(tok2, "! %varid%", varid)) {
dep = true;
} else if (innerParlevel > 0 && Token::Match(tok2, "%var% (") && !test_white_list(tok2->str())) {
bool use = false;
2011-10-13 20:53:06 +02:00
for (const Token *tok3 = tok2->tokAt(2); tok3; tok3 = tok3->next()) {
if (tok3->str() == "(")
tok3 = tok3->link();
else if (tok3->str() == ")")
break;
2011-10-13 20:53:06 +02:00
else 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()->isOp())
dep = true;
}
2011-10-13 20:53:06 +02:00
if (Token::Match(tok, "if ( ! %varid% &&", varid)) {
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, "if(!var)");
2011-10-13 20:53:06 +02:00
} else if (tok->next() &&
tok->next()->link() &&
Token::Match(tok->next()->link()->tokAt(-3), "&& ! %varid%", varid)) {
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, "if(!var)");
2011-10-13 20:53:06 +02:00
} 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")) {
if (Token::simpleMatch(tok, "while ( true )") ||
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 = tok->next()->link();
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) ||
2011-10-13 20:53:06 +02:00
Token::Match(tok, "while ( %varid% != -1 )", varid)) {
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, "while(var)");
tok = tok->next()->link();
continue;
2011-10-13 20:53:06 +02:00
} else if (Token::Match(tok, "while ( %varid% == -1 )", varid) ||
Token::Match(tok, "while ( %varid% < 0 )", varid)) {
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, "while(!var)");
tok = tok->next()->link();
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 = tok->next()->link();
continue;
2011-10-13 20:53:06 +02:00
} else if (varid && Token::simpleMatch(tok, "while (") && notvar(tok->tokAt(2), varid, true)) {
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, "while(!var)");
tok = tok->next()->link();
continue;
}
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, "loop");
2011-01-16 09:57:56 +01:00
2011-10-13 20:53:06 +02:00
if (varid > 0) {
2011-01-16 09:56:04 +01:00
unsigned int parlevel2 = 0;
2011-10-13 20:53:06 +02:00
for (const Token *tok2 = tok->tokAt(2); tok2; tok2 = tok2->next()) {
2011-01-16 09:56:04 +01:00
if (tok2->str() == "(")
++parlevel2;
2011-10-13 20:53:06 +02:00
else if (tok2->str() == ")") {
2011-01-16 09:56:04 +01:00
if (parlevel2 > 0)
--parlevel2;
else
break;
}
2011-10-13 20:53:06 +02:00
if (notvar(tok2, varid)) {
2011-01-16 09:56:04 +01:00
addtoken(&rettail, tok2, "!var");
break;
}
}
}
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-10-13 20:53:06 +02:00
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->tokAt(5);
while (tok2 && tok2->str() != ">")
tok2 = tok2->next();
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;
std::stack<const Token *> f;
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() == "(")
f.push(tok2->previous());
else if (!f.empty() && tok2->str() == ")")
f.pop();
2011-10-13 20:53:06 +02:00
if (tok2->varId() == varid) {
// Read data..
if (!Token::Match(tok2->previous(), "&|(") &&
2011-10-13 20:53:06 +02:00
Token::simpleMatch(tok2->next(), "[")) {
} else if (f.empty() ||
!test_white_list(f.top()->str()) ||
getDeallocationType(f.top(),varid)) {
use = true;
}
}
}
if (use)
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, "use");
addtoken(&rettail, tok, ";");
}
}
// throw..
else if (Token::Match(tok, "try|throw|catch"))
2010-09-04 10:06:34 +02:00
addtoken(&rettail, tok, tok->str());
// Assignment..
2011-10-13 20:53:06 +02:00
if (varid) {
if (Token::simpleMatch(tok, "= {")) {
unsigned int indentlevel2 = 0;
bool use = false;
2011-10-13 20:53:06 +02:00
for (const Token *tok2 = tok; tok2; tok2 = tok2->next()) {
if (tok2->str() == "{")
++indentlevel2;
2011-10-13 20:53:06 +02:00
else if (tok2->str() == "}") {
if (indentlevel2 <= 1)
break;
--indentlevel2;
2011-10-13 20:53:06 +02:00
} else 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, "[)=] %varid% [+;)]", varid) ||
Token::Match(tok, "%var% + %varid%", varid) ||
Token::Match(tok, "%varid% +=|-=", varid) ||
Token::Match(tok, "+=|<< %varid% ;", varid) ||
Token::Match(tok, "= strcpy|strcat|memmove|memcpy ( %varid% ,", varid) ||
2011-10-13 20:53:06 +02:00
Token::Match(tok, "[;{}] %var% [ %varid% ]", varid)) {
addtoken(&rettail, tok, "use");
2011-10-13 20:53:06 +02:00
} else if (Token::Match(tok->previous(), ";|{|}|=|(|,|%op% %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..
2011-10-13 20:53:06 +02:00
if (Token::Match(tok, "%var% (")) {
// 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 (noreturn.find(tok->str()) != noreturn.end())
addtoken(&rettail, tok, "exit");
2011-10-13 20:53:06 +02:00
else if (!test_white_list(tok->str())) {
int innerParlevel = 1;
2011-10-13 20:53:06 +02:00
for (const Token *tok2 = tok->tokAt(2); tok2; tok2 = tok2->next()) {
if (tok2->str() == "(")
++innerParlevel;
2011-10-13 20:53:06 +02:00
else if (tok2->str() == ")") {
--innerParlevel;
if (innerParlevel <= 0)
break;
}
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, "%var% ( 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..
2011-10-13 20:53:06 +02:00
if (Token::Match(tok, "( *| %var%") && 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();
2011-03-06 09:42:16 +01:00
while (Token::Match(tok2, ". %var%"))
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..
2011-10-13 20:53:06 +02:00
if (varid > 0 && Token::Match(tok, "[=(,] & %varid% [.[,)]", varid)) {
2010-09-04 10:06:34 +02:00
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)
{
{
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");
}
}
// 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 (Token::simpleMatch(tok3->next(), "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
2011-10-13 20:53:06 +02:00
for (Token *tok2 = tok; tok2; tok2 = tok2 ? tok2->next() : NULL) {
// 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..
2011-10-13 20:53:06 +02:00
if (Token::Match(tok2->next(), "{ %var% ; }")) {
tok2->deleteNext();
tok2->tokAt(2)->deleteNext();
done = false;
}
2011-10-13 20:53:06 +02:00
if (Token::Match(tok2->next(), "{ %var% %var% ; }")) {
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 %var% ; else %var% ;") &&
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 %var% ;" => "if %var% ;"
2011-10-13 20:53:06 +02:00
else if (Token::Match(tok2->next(), "if ; else %var% ;")) {
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 (Token::simpleMatch(tok2->next(), "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..
2011-10-13 20:53:06 +02:00
if (Token::simpleMatch(tok2->tokAt(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 ;"
2011-10-13 20:53:06 +02:00
if (! _settings->experimental && 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;
}
// 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 ; %var%" => "[;{}] return use ;"
2011-10-13 20:53:06 +02:00
if (Token::Match(tok2, "[;{}] return use ; %var%")) {
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
2011-10-13 20:53:06 +02:00
if (Token::simpleMatch(tok2, "use use")) {
tok2->deleteNext();
2010-09-11 21:32:21 +02:00
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"));
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 |= (_tok->str() == "case");
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"..
2011-10-13 20:53:06 +02:00
if (done && _settings->experimental) {
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;
if ((result = Token::findsimplematch(tokens, "loop alloc ;")) != NULL) {
return 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);
}
2011-10-13 20:53:06 +02:00
if ((result = Token::findmatch(tokens, "alloc ; if|if(var)|ifv return ;")) != NULL) {
return result->tokAt(3);
}
2011-10-13 20:53:06 +02:00
if ((result = Token::findmatch(tokens, "alloc ; alloc|assign|return callfunc| ;")) != NULL) {
return result->tokAt(2);
}
if ((result = Token::findsimplematch(tokens, "; alloc ; if assign ;")) != NULL) {
return result->tokAt(4);
2009-09-01 19:33:17 +02:00
}
if (((result = Token::findsimplematch(tokens, "; alloc ; if dealloc ; }")) != NULL) &&
2011-10-13 20:53:06 +02:00
!result->tokAt(7)) {
return result->tokAt(6);
}
if ((result = Token::findsimplematch(tokens, "alloc ; }")) != NULL) {
if (result->tokAt(3) == NULL)
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;
}
return NULL;
}
// Check for memory leaks for a function variable.
void CheckMemoryLeakInFunction::checkScope(const Token *Tok1, const std::string &varname, unsigned int varid, bool classmember, unsigned int sz)
{
std::list<const Token *> callstack;
AllocType alloctype = No;
AllocType dealloctype = No;
const Token *result;
Token *tok = getcode(Tok1, callstack, varid, alloctype, dealloctype, classmember, sz);
//tok->printOut((std::string("Checkmemoryleak: getcode result for: ") + varname).c_str());
// 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();
}
2011-10-13 20:53:06 +02:00
if ((result = Token::findmatch(tok, "[;{}] dealloc ; use_ ;")) != NULL) {
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") == 0) {
Tokenizer::deleteTokens(tok);
return;
}
simplifycode(tok);
2011-10-13 20:53:06 +02:00
if (_settings->debug && _settings->_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") == 0) {
Tokenizer::deleteTokens(tok);
return;
}
/** @todo handle "goto" */
if (Token::findsimplematch(tok, "goto")) {
Tokenizer::deleteTokens(tok);
return;
}
2011-10-13 20:53:06 +02:00
if ((result = findleak(tok)) != NULL) {
memoryLeak(result, varname, alloctype);
}
else if ((result = Token::findsimplematch(tok, "dealloc ; dealloc ;")) != NULL) {
2009-03-21 17:58:13 +01:00
deallocDeallocError(result->tokAt(2), varname);
}
// detect cases that "simplifycode" don't handle well..
2011-10-13 20:53:06 +02:00
else if (_settings->debugwarnings) {
Token *first = tok;
while (first && first->str() == ";")
first = first->next();
bool noerr = false;
noerr |= Token::simpleMatch(first, "alloc ; }");
noerr |= Token::simpleMatch(first, "alloc ; dealloc ; }");
noerr |= Token::simpleMatch(first, "alloc ; return use ; }");
noerr |= Token::simpleMatch(first, "alloc ; use ; }");
noerr |= Token::simpleMatch(first, "alloc ; use ; return ; }");
noerr |= Token::simpleMatch(first, "alloc ; dealloc ; return ; }");
noerr |= Token::simpleMatch(first, "if alloc ; dealloc ; }");
noerr |= Token::simpleMatch(first, "if alloc ; return use ; }");
noerr |= Token::simpleMatch(first, "if alloc ; use ; }");
noerr |= Token::simpleMatch(first, "alloc ; ifv return ; dealloc ; }");
noerr |= Token::simpleMatch(first, "alloc ; if return ; dealloc; }");
// Unhandled case..
2011-10-13 20:53:06 +02:00
if (! noerr) {
std::ostringstream errmsg;
errmsg << "inconclusive leak of " << varname << ": ";
for (const Token *tok2 = tok; tok2; tok2 = tok2->next())
errmsg << " " << tok2->str();
reportError(_tokenizer->tokens(), Severity::debug, "debug", errmsg.str());
}
}
Tokenizer::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);
//---------------------------------------------------------------------------
void CheckMemoryLeakInFunction::checkReallocUsage()
{
std::list<Scope>::const_iterator scope;
2011-10-13 20:53:06 +02:00
for (scope = symbolDatabase->scopeList.begin(); scope != symbolDatabase->scopeList.end(); ++scope) {
// only check functions
if (scope->type != Scope::eFunction)
continue;
// Record the varid's of the function parameters
std::set<unsigned int> parameterVarIds;
2011-10-13 20:53:06 +02:00
for (const Token *tok2 = scope->classDef->next(); tok2 && tok2->str() != ")"; tok2 = tok2->next()) {
if (tok2->varId() != 0)
parameterVarIds.insert(tok2->varId());
}
const Token *tok = scope->classStart;
const Token *startOfFunction = tok;
// Search for the "var = realloc(var, 100" pattern within this function
unsigned int indentlevel = 1;
2011-10-13 20:53:06 +02:00
for (tok = tok->next(); tok; tok = tok->next()) {
if (tok->str() == "{")
++indentlevel;
2011-10-13 20:53:06 +02:00
else if (tok->str() == "}") {
--indentlevel;
if (indentlevel == 0)
break;
}
if (tok->varId() > 0 &&
Token::Match(tok, "%var% = realloc|g_try_realloc ( %var% , %any%") &&
tok->varId() == tok->tokAt(4)->varId() &&
2011-10-13 20:53:06 +02:00
parameterVarIds.find(tok->varId()) == parameterVarIds.end()) {
// Check that another copy of the pointer wasn't saved earlier in the function
2011-01-13 07:33:46 +01:00
if (Token::findmatch(startOfFunction, "%var% = %varid% ;", tok->varId()) ||
Token::findmatch(startOfFunction, "[{};] %varid% = %var% [;=]", tok->varId()))
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
if (Token::Match(tokEndRealloc->next(), "; if ( ! %varid% ) {", tok->varId())) {
const Token* tokEndBrace = tokEndRealloc->linkAt(7);
2011-01-13 07:33:46 +01:00
if (tokEndBrace && Token::simpleMatch(tokEndBrace->tokAt(-2), ") ;") &&
Token::Match(tokEndBrace->linkAt(-2)->tokAt(-2), "{|}|; %var% ("))
2011-01-13 07:33:46 +01:00
continue;
}
memleakUponReallocFailureError(tok, tok->str());
} else if (tok->next()->varId() > 0 &&
2011-10-13 20:53:06 +02:00
(Token::Match(tok, "* %var% = realloc|g_try_realloc ( * %var% , %any%") &&
tok->next()->varId() == tok->tokAt(6)->varId())&&
parameterVarIds.find(tok->next()->varId()) == parameterVarIds.end()) {
// Check that another copy of the pointer wasn't saved earlier in the function
if (Token::findmatch(startOfFunction, "%var% = * %varid% ;", tok->next()->varId()) ||
Token::findmatch(startOfFunction, "[{};] * %varid% = %var% [;=]", 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), "{|}|; %var% ("))
continue;
}
memleakUponReallocFailureError(tok->next(), tok->strAt(1));
}
}
}
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Checks for memory leaks inside function..
//---------------------------------------------------------------------------
void CheckMemoryLeakInFunction::parseFunctionScope(const Token *tok, const Token *tok1, const bool classmember)
{
// Check locking/unlocking of global resources..
checkScope(tok->next(), "", 0, classmember, 1);
// Locate parameters and check their usage..
2011-10-13 20:53:06 +02:00
for (const Token *tok2 = tok1; tok2; tok2 = tok2->next()) {
if (tok2 == tok)
break;
if (tok2->str() == ")")
break;
2011-10-13 20:53:06 +02:00
if (Token::Match(tok2, "[(,] %type% * %var% [,)]") && tok2->next()->isStandardType()) {
const std::string varname(tok2->strAt(3));
const unsigned int varid = tok2->tokAt(3)->varId();
const unsigned int sz = _tokenizer->sizeOfType(tok2->next());
checkScope(tok->next(), varname, varid, classmember, sz);
}
}
// Locate variable declarations and check their usage..
unsigned int indentlevel = 0;
2011-10-13 20:53:06 +02:00
do {
if (tok->str() == "{")
++indentlevel;
2011-10-13 20:53:06 +02:00
else if (tok->str() == "}") {
if (indentlevel <= 1)
break;
--indentlevel;
}
// Skip these weird blocks... "( { ... } )"
2011-10-13 20:53:06 +02:00
if (Token::simpleMatch(tok, "( {")) {
tok = tok->link();
if (!tok)
break;
continue;
}
if (!Token::Match(tok, "[{};] %type%"))
continue;
// Don't check static/extern variables
if (Token::Match(tok->next(), "static|extern"))
continue;
// return/else is not part of a variable declaration..
if (Token::Match(tok->next(), "return|else"))
continue;
unsigned int sz = _tokenizer->sizeOfType(tok->next());
if (sz < 1)
sz = 1;
2011-10-13 20:53:06 +02:00
if (Token::Match(tok, "[{};] %type% * const| %var% [;=]")) {
const Token *vartok = tok->tokAt(tok->strAt(3) != "const" ? 3 : 4);
checkScope(tok->next(), vartok->str(), vartok->varId(), classmember, sz);
}
2011-10-13 20:53:06 +02:00
else if (Token::Match(tok, "[{};] %type% %type% * const| %var% [;=]")) {
const Token *vartok = tok->tokAt(tok->strAt(4) != "const" ? 4 : 5);
checkScope(tok->next(), vartok->str(), vartok->varId(), classmember, sz);
}
2011-10-13 20:53:06 +02:00
else if (Token::Match(tok, "[{};] int %var% [;=]")) {
const Token *vartok = tok->tokAt(2);
checkScope(tok->next(), vartok->str(), vartok->varId(), classmember, sz);
}
} while (NULL != (tok = tok->next()));
}
void CheckMemoryLeakInFunction::check()
{
// fill the "noreturn"
parse_noreturn();
std::list<Scope>::const_iterator scope;
2011-10-13 20:53:06 +02:00
for (scope = symbolDatabase->scopeList.begin(); scope != symbolDatabase->scopeList.end(); ++scope) {
// only check functions
if (scope->type != Scope::eFunction)
continue;
const Token *tok = scope->classStart;
const Token *tok1 = scope->classDef->next();
bool classmember = scope->functionOf != NULL;
parseFunctionScope(tok, tok1, classmember);
}
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Checks for memory leaks in classes..
//---------------------------------------------------------------------------
void CheckMemoryLeakInClass::check()
{
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
std::list<Scope>::const_iterator scope;
2011-10-13 20:53:06 +02:00
for (scope = symbolDatabase->scopeList.begin(); scope != symbolDatabase->scopeList.end(); ++scope) {
// only check classes and structures
2011-10-13 20:53:06 +02:00
if (scope->isClassOrStruct()) {
std::list<Variable>::const_iterator var;
2011-10-13 20:53:06 +02:00
for (var = scope->varlist.begin(); var != scope->varlist.end(); ++var) {
if (!var->isStatic() && var->nameToken()->previous()->str() == "*") {
// allocation but no deallocation of private variables in public function..
2011-10-13 20:53:06 +02:00
if (var->nameToken()->tokAt(-2)->isStandardType()) {
if (var->isPrivate())
checkPublicFunctions(&(*scope), var->nameToken());
variable(&(*scope), var->nameToken());
}
// known class?
2011-10-13 20:53:06 +02:00
else if (var->type()) {
// not derived?
2011-10-13 20:53:06 +02:00
if (var->type()->derivedFrom.empty()) {
if (var->isPrivate())
checkPublicFunctions(&(*scope), var->nameToken());
variable(&(*scope), var->nameToken());
}
}
}
}
}
}
}
void CheckMemoryLeakInClass::variable(const Scope *scope, const Token *tokVarname)
{
const std::string varname = tokVarname->strAt(0);
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 Token *functionToken = func->token;
const bool constructor = func->type == Function::eConstructor;
const bool destructor = func->type == Function::eDestructor;
unsigned int indent = 0;
bool initlist = false;
2011-10-13 20:53:06 +02:00
for (const Token *tok = functionToken; tok; tok = tok->next()) {
if (tok->str() == "{")
++indent;
2011-10-13 20:53:06 +02:00
else if (tok->str() == "}") {
if (indent <= 1)
break;
--indent;
2011-10-13 20:53:06 +02:00
} else if (indent == 0 && Token::simpleMatch(tok, ") :"))
initlist = true;
2011-10-13 20:53:06 +02:00
else if (initlist || indent > 0) {
if (indent == 0) {
if (!Token::Match(tok, (":|, " + varname + " (").c_str()))
continue;
}
// Allocate..
if (indent == 0 || Token::simpleMatch(tok, (varname + " =").c_str())) {
// var1 = var2 = ...
// bail out
if (Token::simpleMatch(tok->previous(), "="))
return;
// Foo::var1 = ..
// bail out when not same class
if (Token::simpleMatch(tok->previous(), "::") &&
tok->strAt(-2) != scope->className)
return;
AllocType alloc = getAllocationType(tok->tokAt((indent > 0) ? 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;
std::list<const Token *> callstack;
2011-10-13 20:53:06 +02:00
if (alloc != CheckMemoryLeak::Many && Dealloc != CheckMemoryLeak::No && Dealloc != CheckMemoryLeak::Many && Dealloc != alloc) {
callstack.push_back(tok);
mismatchAllocDealloc(callstack, classname + "::" + varname);
callstack.pop_back();
}
Alloc = alloc;
}
}
if (indent == 0)
continue;
// Deallocate..
AllocType dealloc = getDeallocationType(tok, varname);
2011-10-13 20:53:06 +02:00
if (dealloc == No) {
std::string temp = scope->className + " :: " + varname;
dealloc = getDeallocationType(tok, temp);
}
2011-10-13 20:53:06 +02:00
if (dealloc == No) {
std::string temp = "this . " + varname;
dealloc = getDeallocationType(tok, temp);
}
// 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;
std::list<const Token *> callstack;
2011-10-13 20:53:06 +02:00
if (dealloc != CheckMemoryLeak::Many && Alloc != CheckMemoryLeak::No && Alloc != Many && Alloc != dealloc) {
2009-02-01 16:47:36 +01:00
callstack.push_back(tok);
mismatchAllocDealloc(callstack, classname + "::" + varname);
2009-02-01 16:47:36 +01:00
callstack.pop_back();
}
Dealloc = dealloc;
}
// Function call .. possible deallocation
2011-10-13 20:53:06 +02:00
else if (Token::Match(tok->previous(), "[{};] %var% (")) {
2011-12-08 21:28:34 +01:00
if (!CheckMemoryLeakInFunction::test_white_list(tok->str())) {
return;
}
}
}
}
}
2011-10-13 20:53:06 +02:00
if (allocInConstructor && !deallocInDestructor) {
2011-12-08 21:28:34 +01:00
memoryLeak(tokVarname, classname + "::" + varname, Alloc);
2011-10-13 20:53:06 +02:00
} else if (Alloc != CheckMemoryLeak::No && Dealloc == CheckMemoryLeak::No) {
2011-12-08 21:28:34 +01:00
memoryLeak(tokVarname, classname + "::" + varname, Alloc);
}
}
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 (!_settings->isEnabled("style"))
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::eConstructor &&
2011-10-13 20:53:06 +02:00
func->access == Public && func->hasBody) {
const Token *tok2 = func->token;
while (tok2->str() != "{")
tok2 = tok2->next();
2011-10-13 20:53:06 +02:00
if (Token::Match(tok2, "{|}|; %varid% =", varid)) {
const CheckMemoryLeak::AllocType alloc = getAllocationType(tok2->tokAt(3), varid);
if (alloc != CheckMemoryLeak::No)
publicAllocationError(tok2, tok2->strAt(1));
2011-10-13 20:53:06 +02:00
} else if (Token::Match(tok2, "{|}|; %type% :: %varid% =", varid) &&
tok2->next()->str() == scope->className) {
const CheckMemoryLeak::AllocType alloc = getAllocationType(tok2->tokAt(5), varid);
if (alloc != CheckMemoryLeak::No)
publicAllocationError(tok2, tok2->strAt(3));
}
}
}
}
void CheckMemoryLeakInClass::publicAllocationError(const Token *tok, const std::string &varname)
{
reportError(tok, Severity::warning, "publicAllocationError", "Possible leak in public function. The pointer '" + varname + "' is not deallocated before it is allocated.");
}
void CheckMemoryLeakStructMember::check()
{
unsigned int indentlevel1 = 0;
2011-10-13 20:53:06 +02:00
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
if (tok->str() == "{")
++indentlevel1;
else if (tok->str() == "}")
--indentlevel1;
if (indentlevel1 == 0)
continue;
// check struct variables..
2011-10-13 20:53:06 +02:00
if (Token::Match(tok, "struct|;|{|} %type% * %var% [=;]")) {
checkStructVariable(tok->tokAt(3));
2011-10-13 20:53:06 +02:00
} else if (Token::Match(tok, "struct|;|{|} %type% %var% ;")) {
checkStructVariable(tok->tokAt(2));
}
}
}
bool CheckMemoryLeakStructMember::isMalloc(const Token *vartok)
{
const unsigned int varid(vartok->varId());
bool alloc = false;
unsigned int indentlevel2 = 0;
2011-10-13 20:53:06 +02:00
for (const Token *tok2 = vartok; tok2; tok2 = tok2->next()) {
if (tok2->str() == "{")
++indentlevel2;
2011-10-13 20:53:06 +02:00
else if (tok2->str() == "}") {
if (indentlevel2 == 0)
break;
--indentlevel2;
2011-10-13 20:53:06 +02:00
} else if (Token::Match(tok2, "= %varid% [;=]", varid)) {
return false;
2011-10-13 20:53:06 +02:00
} else if (Token::Match(tok2, "%varid% = malloc|kmalloc (", varid)) {
alloc = true;
}
}
return alloc;
}
void CheckMemoryLeakStructMember::checkStructVariable(const Token * const vartok)
{
// This should be in the CheckMemoryLeak base class
std::set<std::string> ignoredFunctions;
ignoredFunctions.insert("if");
ignoredFunctions.insert("for");
ignoredFunctions.insert("while");
ignoredFunctions.insert("malloc");
if (vartok->varId() == 0)
return;
// Is struct variable a pointer?
2011-10-13 20:53:06 +02:00
if (vartok->strAt(-1) == "*") {
// Check that variable is allocated with malloc
if (!isMalloc(vartok))
return;
2011-10-13 20:53:06 +02:00
} else {
// If file extension is not .c then a destructor might cleanup
// members
const std::string &fname = _tokenizer->getFiles()->at(0);
if (fname.find(".c") != fname.size() - 2U)
return;
}
// Check struct..
unsigned int indentlevel2 = 0;
2011-10-13 20:53:06 +02:00
for (const Token *tok2 = vartok; tok2; 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% [,)]", vartok->varId()))
break;
// Struct member is allocated => check if it is also properly deallocated..
2011-10-13 20:53:06 +02:00
else if (Token::Match(tok2->previous(), "[;{}] %varid% . %var% = malloc|strdup|kmalloc (", vartok->varId())) {
const unsigned int structid(vartok->varId());
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) {
2011-12-08 21:28:34 +01:00
memoryLeak(tok3, vartok->str() + "." + tok2->strAt(2), Malloc);
break;
}
--indentlevel3;
}
// Deallocating the struct member..
2011-10-13 20:53:06 +02:00
else if (Token::Match(tok3, "free|kfree ( %var% . %varid% )", structmemberid)) {
// 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) {
// debug info
const std::string tok3str_(tok3->str());
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..
2011-10-13 20:53:06 +02:00
else if (indentlevel2 == 0 && Token::Match(tok3, "free|kfree ( %varid% )", structid)) {
2011-12-08 21:28:34 +01:00
memoryLeak(tok3, vartok->str() + "." + tok2->strAt(2), Malloc);
break;
}
// failed allocation => skip code..
2011-10-13 20:53:06 +02:00
else if (Token::Match(tok3, "if ( ! %var% . %varid% )", 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
2011-10-13 20:53:06 +02:00
else if (Token::Match(tok3, "if ( %var% . %varid% ) {", structmemberid)) {
// 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) &&
2011-10-13 20:53:06 +02:00
!Token::Match(tok3, "return & %varid% .", structid)) {
2011-12-08 21:28:34 +01:00
memoryLeak(tok3, vartok->str() + "." + tok2->strAt(2), Malloc);
}
break;
}
// struct assignment..
2011-10-13 20:53:06 +02:00
else if (Token::Match(tok3, "= %varid% ;", structid)) {
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..
2011-10-13 20:53:06 +02:00
else if (Token::Match(tok3, "%var% (")) {
// Calling non-function / function that doesn't deallocate?
if (ignoredFunctions.find(tok3->str()) != ignoredFunctions.end())
continue;
// Check if the struct is used..
bool deallocated = false;
unsigned int parlevel = 0;
2011-10-13 20:53:06 +02:00
for (const Token *tok4 = tok3; tok4; tok4 = tok4->next()) {
if (tok4->str() == "(")
++parlevel;
2011-10-13 20:53:06 +02:00
else if (tok4->str() == ")") {
if (parlevel <= 1)
break;
--parlevel;
}
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;
}
2011-10-13 20:53:06 +02:00
if (Token::Match(tok4, "[(,] &| %varid% . %var% [,)]", structid)) {
/** @todo check if the function deallocates the memory */
deallocated = true;
break;
}
}
if (deallocated)
break;
}
}
}
}
}
2009-03-21 17:58:13 +01:00
2011-11-26 21:02:04 +01:00
#include "checkuninitvar.h" // CheckUninitVar::analyse
void CheckMemoryLeakNoVar::check()
{
std::set<std::string> uvarFunctions;
{
const CheckUninitVar c(_tokenizer, _settings, _errorLogger);
c.analyse(_tokenizer->tokens(), uvarFunctions);
}
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
std::list<Scope>::const_iterator scope;
2011-10-13 20:53:06 +02:00
for (scope = symbolDatabase->scopeList.begin(); scope != symbolDatabase->scopeList.end(); ++scope) {
// only check functions
if (scope->type != Scope::eFunction)
continue;
// goto the "}" that ends the executable scope..
const Token *tok = scope->classEnd;
// parse the executable scope until tok is reached...
2011-10-13 20:53:06 +02:00
for (const Token *tok2 = tok->link(); tok2 && tok2 != tok; tok2 = tok2->next()) {
// allocating memory in parameter for function call..
if (Token::Match(tok2, "[(,] %var% (") && Token::Match(tok2->linkAt(2), ") [,)]")) {
const AllocType allocType = getAllocationType(tok2->next(), 0);
2011-10-13 20:53:06 +02:00
if (allocType != No) {
// locate outer function call..
2011-10-13 20:53:06 +02:00
for (const Token *tok3 = tok2; tok3; tok3 = tok3->previous()) {
if (tok3->str() == "(") {
// Is it a function call..
if (Token::Match(tok3->tokAt(-2), "[;{}] %var% (")) {
const std::string functionName = tok3->strAt(-1);
if (functionName == "delete" ||
functionName == "free" ||
functionName == "fclose" ||
functionName == "realloc")
break;
2011-10-13 20:53:06 +02:00
if (CheckMemoryLeakInFunction::test_white_list(functionName)) {
functionCallLeak(tok2, tok2->strAt(1), functionName);
break;
}
2011-10-13 20:53:06 +02:00
if (uvarFunctions.find(functionName) != uvarFunctions.end()) {
functionCallLeak(tok2, tok2->strAt(1), functionName);
break;
}
}
break;
2011-10-13 20:53:06 +02:00
} else if (tok3->str() == ")")
tok3 = tok3->link();
else if (Token::Match(tok3, "[;{}]"))
break;
}
}
}
}
}
}
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.");
}