2008-12-18 22:28:57 +01:00
|
|
|
/*
|
2009-01-21 21:04:20 +01:00
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2011-01-09 20:33:36 +01:00
|
|
|
* Copyright (C) 2007-2011 Daniel Marjamäki and Cppcheck team.
|
2008-12-18 22:28:57 +01:00
|
|
|
*
|
|
|
|
* 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
|
2009-09-27 17:08:31 +02:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2008-12-18 22:28:57 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
2009-06-08 18:51:17 +02:00
|
|
|
#ifndef checkmemoryleakH
|
|
|
|
#define checkmemoryleakH
|
2008-12-18 22:28:57 +01:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2009-06-08 20:54:49 +02:00
|
|
|
/**
|
2010-03-13 22:16:06 +01:00
|
|
|
* @file
|
|
|
|
*
|
|
|
|
* %Check for memory leaks
|
2009-06-08 20:54:49 +02:00
|
|
|
*
|
|
|
|
* The checking is split up into two specialized classes.
|
2009-07-17 10:49:01 +02:00
|
|
|
* - CheckMemoryLeakInFunction can detect when a function variable is allocated but not deallocated properly.
|
|
|
|
* - CheckMemoryLeakInClass can detect when a class variable is allocated but not deallocated properly.
|
2010-03-13 21:12:18 +01:00
|
|
|
* - CheckMemoryLeakStructMember checks allocation/deallocation of structs and struct members
|
2009-06-08 20:54:49 +02:00
|
|
|
*/
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-03-20 18:16:21 +01:00
|
|
|
#include "check.h"
|
2010-11-23 18:41:07 +01:00
|
|
|
#include "symboldatabase.h"
|
2009-03-20 18:16:21 +01:00
|
|
|
|
2008-12-18 22:28:57 +01:00
|
|
|
#include <list>
|
2009-03-20 18:16:21 +01:00
|
|
|
#include <string>
|
2008-12-18 22:28:57 +01:00
|
|
|
#include <vector>
|
|
|
|
|
2009-03-20 18:16:21 +01:00
|
|
|
class Token;
|
|
|
|
|
2010-03-13 21:12:18 +01:00
|
|
|
/// @addtogroup Core
|
|
|
|
/// @{
|
|
|
|
|
2009-07-17 10:49:01 +02:00
|
|
|
/** @brief Base class for memory leaks checking */
|
2009-06-08 20:20:43 +02:00
|
|
|
class CheckMemoryLeak
|
|
|
|
{
|
2009-07-13 15:07:26 +02:00
|
|
|
private:
|
2010-03-13 21:12:18 +01:00
|
|
|
/** For access to the tokens */
|
2009-07-13 15:07:26 +02:00
|
|
|
const Tokenizer * const tokenizer;
|
2010-03-13 21:12:18 +01:00
|
|
|
|
|
|
|
/** ErrorLogger used to report errors */
|
2009-07-13 15:07:26 +02:00
|
|
|
ErrorLogger * const errorLogger;
|
|
|
|
|
2009-07-17 10:49:01 +02:00
|
|
|
/** Disable the default constructors */
|
|
|
|
CheckMemoryLeak();
|
|
|
|
|
|
|
|
/** Disable the default constructors */
|
|
|
|
CheckMemoryLeak(const CheckMemoryLeak &);
|
|
|
|
|
2009-09-13 09:03:48 +02:00
|
|
|
/** disable assignment operator */
|
|
|
|
void operator=(const CheckMemoryLeak &);
|
|
|
|
|
2009-07-17 10:49:01 +02:00
|
|
|
/**
|
|
|
|
* Report error. Similar with the function Check::reportError
|
|
|
|
* @param location the token where the error occurs
|
|
|
|
* @param severity the severity of the bug
|
|
|
|
* @param id type of message
|
|
|
|
* @param msg text
|
|
|
|
*/
|
2010-07-14 16:30:03 +02:00
|
|
|
void reportErr(const Token *location, Severity::SeverityType severity, const std::string &id, const std::string &msg) const;
|
2009-07-17 10:49:01 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Report error. Similar with the function Check::reportError
|
|
|
|
* @param callstack callstack of error
|
|
|
|
* @param severity the severity of the bug
|
|
|
|
* @param id type of message
|
|
|
|
* @param msg text
|
|
|
|
*/
|
2010-07-14 16:30:03 +02:00
|
|
|
void reportErr(const std::list<const Token *> &callstack, Severity::SeverityType severity, const std::string &id, const std::string &msg) const;
|
2009-07-13 15:07:26 +02:00
|
|
|
|
2009-06-15 17:44:59 +02:00
|
|
|
public:
|
2009-07-13 15:07:26 +02:00
|
|
|
CheckMemoryLeak(const Tokenizer *t, ErrorLogger *e)
|
2010-04-15 20:08:51 +02:00
|
|
|
: tokenizer(t), errorLogger(e)
|
2009-07-13 15:07:26 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2009-06-08 20:20:43 +02:00
|
|
|
|
2010-03-13 21:12:18 +01:00
|
|
|
/** @brief What type of allocation are used.. the "Many" means that several types of allocation and deallocation are used */
|
2009-06-21 17:01:43 +02:00
|
|
|
enum AllocType { No, Malloc, gMalloc, New, NewArray, File, Fd, Pipe, Dir, Many };
|
2009-06-08 20:20:43 +02:00
|
|
|
|
2010-05-16 19:55:16 +02:00
|
|
|
void memoryLeak(const Token *tok, const std::string &varname, AllocType alloctype);
|
2009-07-19 16:51:31 +02:00
|
|
|
|
|
|
|
/**
|
2010-03-13 21:12:18 +01:00
|
|
|
* @brief Get type of deallocation at given position
|
2009-08-19 19:42:07 +02:00
|
|
|
* @param tok position
|
2010-12-07 07:07:36 +01:00
|
|
|
* @param varname variable name
|
2009-08-19 19:42:07 +02:00
|
|
|
* @return type of deallocation
|
2009-07-19 16:51:31 +02:00
|
|
|
*/
|
2010-12-07 07:07:36 +01:00
|
|
|
AllocType getDeallocationType(const Token *tok, const std::string &varname) const;
|
2009-08-19 19:42:07 +02:00
|
|
|
|
|
|
|
/**
|
2010-03-13 21:12:18 +01:00
|
|
|
* @brief Get type of deallocation at given position
|
2009-08-19 19:42:07 +02:00
|
|
|
* @param tok position
|
2009-10-15 18:52:29 +02:00
|
|
|
* @param varid variable id
|
2009-08-19 19:42:07 +02:00
|
|
|
* @return type of deallocation
|
|
|
|
*/
|
2009-08-25 21:25:27 +02:00
|
|
|
AllocType getDeallocationType(const Token *tok, unsigned int varid) const;
|
2009-07-19 16:51:31 +02:00
|
|
|
|
|
|
|
/**
|
2010-03-13 21:12:18 +01:00
|
|
|
* @brief Get type of allocation at given position
|
2009-07-19 16:51:31 +02:00
|
|
|
*/
|
2011-03-23 18:45:47 +01:00
|
|
|
AllocType getAllocationType(const Token *tok2, unsigned int varid, std::list<const Token *> *callstack = NULL) const;
|
2009-07-19 16:51:31 +02:00
|
|
|
|
|
|
|
/**
|
2010-03-13 21:12:18 +01:00
|
|
|
* @brief Get type of reallocation at given position
|
2009-07-19 16:51:31 +02:00
|
|
|
*/
|
2009-08-25 21:25:27 +02:00
|
|
|
AllocType getReallocationType(const Token *tok2, unsigned int varid) const;
|
2009-07-19 16:51:31 +02:00
|
|
|
|
2010-03-13 21:12:18 +01:00
|
|
|
/**
|
|
|
|
* @brief Is a typename the name of a class?
|
|
|
|
* @param _tokenizer tokenizer
|
|
|
|
* @param typestr type name
|
|
|
|
* @return true if the type name is the name of a class
|
|
|
|
*/
|
2009-06-08 20:20:43 +02:00
|
|
|
bool isclass(const Tokenizer *_tokenizer, const Token *typestr) const;
|
|
|
|
|
2010-05-16 19:55:16 +02:00
|
|
|
void memleakError(const Token *tok, const std::string &varname);
|
|
|
|
void resourceLeakError(const Token *tok, const std::string &varname);
|
2009-06-08 20:20:43 +02:00
|
|
|
|
2010-03-13 21:12:18 +01:00
|
|
|
/**
|
|
|
|
* @brief Report error: deallocating a deallocated pointer
|
|
|
|
* @param tok token where error occurs
|
|
|
|
* @param varname name of variable
|
|
|
|
*/
|
2009-06-08 20:20:43 +02:00
|
|
|
void deallocDeallocError(const Token *tok, const std::string &varname);
|
|
|
|
void deallocuseError(const Token *tok, const std::string &varname);
|
|
|
|
void mismatchSizeError(const Token *tok, const std::string &sz);
|
2010-05-16 19:55:16 +02:00
|
|
|
void mismatchAllocDealloc(const std::list<const Token *> &callstack, const std::string &varname);
|
2010-05-18 07:46:48 +02:00
|
|
|
void memleakUponReallocFailureError(const Token *tok, const std::string &varname);
|
2009-06-08 20:20:43 +02:00
|
|
|
|
2009-06-15 17:44:59 +02:00
|
|
|
/** What type of allocated memory does the given function return? */
|
2011-03-23 18:45:47 +01:00
|
|
|
AllocType functionReturnType(const Token *tok, std::list<const Token *> *callstack = NULL) const;
|
2010-06-05 01:58:50 +02:00
|
|
|
|
|
|
|
/** Function allocates pointed-to argument (a la asprintf)? */
|
2010-06-17 03:29:55 +02:00
|
|
|
const char *functionArgAlloc(const Token *tok, unsigned int targetpar, AllocType &allocType) const;
|
2009-06-08 20:20:43 +02:00
|
|
|
};
|
|
|
|
|
2010-03-13 21:12:18 +01:00
|
|
|
/// @}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// @addtogroup Checks
|
|
|
|
/// @{
|
2009-06-08 20:20:43 +02:00
|
|
|
|
|
|
|
|
2009-06-08 20:54:49 +02:00
|
|
|
/**
|
2010-03-13 22:16:06 +01:00
|
|
|
* @brief %CheckMemoryLeakInFunction detects when a function variable is allocated but not deallocated properly.
|
2009-06-08 20:54:49 +02:00
|
|
|
*
|
|
|
|
* The checking is done by looking at each function variable separately. By repeating these 4 steps over and over:
|
2009-07-17 10:49:01 +02:00
|
|
|
* -# locate a function variable
|
|
|
|
* -# create a simple token list that describes the usage of the function variable.
|
|
|
|
* -# simplify the token list.
|
|
|
|
* -# finally, check if the simplified token list contain any leaks.
|
2009-06-08 20:54:49 +02:00
|
|
|
*/
|
2009-06-08 20:20:43 +02:00
|
|
|
|
2009-12-22 21:56:00 +01:00
|
|
|
class CheckMemoryLeakInFunction : private Check, public CheckMemoryLeak
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
public:
|
2010-03-13 21:12:18 +01:00
|
|
|
/** @brief This constructor is used when registering this class */
|
2011-02-02 10:29:10 +01:00
|
|
|
CheckMemoryLeakInFunction() : Check(myName()), CheckMemoryLeak(0, 0), symbolDatabase(NULL)
|
2009-08-22 20:23:03 +02:00
|
|
|
{ }
|
2009-03-20 18:16:21 +01:00
|
|
|
|
2010-03-13 21:12:18 +01:00
|
|
|
/** @brief This constructor is used when running checks */
|
2010-04-08 22:56:34 +02:00
|
|
|
CheckMemoryLeakInFunction(const Tokenizer *tokenizr, const Settings *settings, ErrorLogger *errLog)
|
2011-02-02 10:29:10 +01:00
|
|
|
: Check(myName(), tokenizr, settings, errLog), CheckMemoryLeak(tokenizr, errLog)
|
2010-12-08 07:49:01 +01:00
|
|
|
{
|
|
|
|
// get the symbol database
|
2010-12-29 12:43:29 +01:00
|
|
|
if (tokenizr)
|
|
|
|
symbolDatabase = tokenizr->getSymbolDatabase();
|
|
|
|
else
|
|
|
|
symbolDatabase = 0;
|
2010-12-08 07:49:01 +01:00
|
|
|
}
|
2009-03-20 18:16:21 +01:00
|
|
|
|
2010-03-13 21:12:18 +01:00
|
|
|
/** @brief run all simplified checks */
|
2010-04-08 22:56:34 +02:00
|
|
|
void runSimplifiedChecks(const Tokenizer *tokenizr, const Settings *settings, ErrorLogger *errLog)
|
2009-03-20 18:16:21 +01:00
|
|
|
{
|
2010-10-19 21:54:15 +02:00
|
|
|
// Don't use these check for Java and C# programs..
|
|
|
|
if (tokenizr->getFiles()->at(0).find(".java") != std::string::npos ||
|
|
|
|
tokenizr->getFiles()->at(0).find(".cs") != std::string::npos)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-04-08 22:56:34 +02:00
|
|
|
CheckMemoryLeakInFunction checkMemoryLeak(tokenizr, settings, errLog);
|
2010-05-18 07:46:48 +02:00
|
|
|
checkMemoryLeak.checkReallocUsage();
|
2009-06-08 20:20:43 +02:00
|
|
|
checkMemoryLeak.check();
|
2009-03-20 18:16:21 +01:00
|
|
|
}
|
|
|
|
|
2010-03-13 21:12:18 +01:00
|
|
|
/** @brief Unit testing : testing the white list */
|
2010-02-14 19:58:17 +01:00
|
|
|
static bool test_white_list(const std::string &funcname);
|
2010-01-18 21:34:11 +01:00
|
|
|
|
2010-03-13 21:12:18 +01:00
|
|
|
/** @brief Perform checking */
|
2009-06-08 20:20:43 +02:00
|
|
|
void check();
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2010-05-18 07:46:48 +02:00
|
|
|
/**
|
|
|
|
* Checking for a memory leak caused by improper realloc usage.
|
|
|
|
*/
|
|
|
|
void checkReallocUsage();
|
|
|
|
|
2009-12-30 21:29:54 +01:00
|
|
|
/**
|
2010-03-13 22:16:06 +01:00
|
|
|
* @brief %Check all variables in function scope
|
2010-04-24 22:24:03 +02:00
|
|
|
* @param tok The first '{' token of the function body
|
|
|
|
* @param tok1 The '(' token in the function declaration
|
2009-12-30 21:29:54 +01:00
|
|
|
* @param classmember Is this function a class member?
|
|
|
|
*/
|
2010-04-24 22:24:03 +02:00
|
|
|
void parseFunctionScope(const Token *tok, const Token *tok1, const bool classmember);
|
2009-12-30 21:29:54 +01:00
|
|
|
|
2010-03-13 21:12:18 +01:00
|
|
|
/**
|
2010-03-13 22:16:06 +01:00
|
|
|
* @brief %Check if there is a "p = foo(p, .." and foo returns the argument (p)
|
2010-03-13 21:12:18 +01:00
|
|
|
* @param tok token to the ";" before the statement
|
|
|
|
* @param varid varid to check
|
|
|
|
*/
|
2009-08-25 21:25:27 +02:00
|
|
|
bool matchFunctionsThatReturnArg(const Token *tok, unsigned int varid) const;
|
2008-12-18 22:28:57 +01:00
|
|
|
|
|
|
|
/**
|
2010-03-13 22:16:06 +01:00
|
|
|
* @brief %Check if there is a "!var" match inside a condition
|
2009-06-08 20:20:43 +02:00
|
|
|
* @param tok first token to match
|
2009-10-15 18:52:29 +02:00
|
|
|
* @param varid variabla id
|
2009-06-08 20:20:43 +02:00
|
|
|
* @param endpar if this is true the "!var" must be followed by ")"
|
|
|
|
* @return true if match
|
2008-12-18 22:28:57 +01:00
|
|
|
*/
|
2009-08-25 21:25:27 +02:00
|
|
|
bool notvar(const Token *tok, unsigned int varid, bool endpar = false) const;
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-07-17 10:49:01 +02:00
|
|
|
/**
|
|
|
|
* Inspect a function call. the call_func and getcode are recursive
|
|
|
|
* @param tok token where the function call occurs
|
|
|
|
* @param callstack callstack
|
2009-10-15 18:52:29 +02:00
|
|
|
* @param varid variable id to check
|
2009-07-17 10:49:01 +02:00
|
|
|
* @param alloctype if memory is allocated, this indicates the type of allocation
|
|
|
|
* @param dealloctype if memory is deallocated, this indicates the type of deallocation
|
2010-06-05 01:58:50 +02:00
|
|
|
* @param allocpar if function allocates varid parameter
|
2009-07-17 10:49:01 +02:00
|
|
|
* @param sz not used by call_func - see getcode
|
|
|
|
* @return These are the possible return values:
|
|
|
|
* - NULL : no significant code
|
|
|
|
* - "recursive" : recursive function
|
|
|
|
* - "alloc" : the function returns allocated memory
|
|
|
|
* - "dealloc" : the function deallocates the variable
|
|
|
|
* - "dealloc_"
|
|
|
|
* - "use" : the variable is used (unknown usage of the variable => the checking bails out)
|
2010-03-13 21:12:18 +01:00
|
|
|
* - "callfunc" : a function call with unknown side effects
|
2009-07-17 10:49:01 +02:00
|
|
|
* - "&use"
|
|
|
|
*/
|
2010-06-05 01:58:50 +02:00
|
|
|
const char * call_func(const Token *tok, std::list<const Token *> callstack, const unsigned int varid, AllocType &alloctype, AllocType &dealloctype, bool &allocpar, unsigned int sz);
|
2008-12-18 22:28:57 +01:00
|
|
|
|
|
|
|
/**
|
2009-07-17 10:49:01 +02:00
|
|
|
* Extract a new tokens list that is easier to parse than the "_tokenizer->tokens()", the
|
|
|
|
* extracted tokens list describes how the given variable is used.
|
|
|
|
* The getcode and call_func are recursive
|
2008-12-18 22:28:57 +01:00
|
|
|
* @param tok start parse token
|
|
|
|
* @param callstack callstack
|
2009-10-15 18:52:29 +02:00
|
|
|
* @param varid variable id
|
2009-07-17 10:49:01 +02:00
|
|
|
* @param alloctype keep track of what type of allocation is used
|
|
|
|
* @param dealloctype keeps track of what type of deallocation is used
|
|
|
|
* @param classmember should be set if the inspected function is a class member
|
|
|
|
* @param sz size of type, used to check for mismatching size of allocation. for example "int *a;" => the sz is "sizeof(int)"
|
2008-12-18 22:28:57 +01:00
|
|
|
* @return Newly allocated token array. Caller needs to release reserved
|
|
|
|
* memory by calling Tokenizer::deleteTokens(returnValue);
|
2009-07-17 10:49:01 +02:00
|
|
|
* Returned tokens:
|
|
|
|
* - alloc : the variable is allocated
|
|
|
|
* - assign : the variable is assigned a new value
|
2010-03-13 21:12:18 +01:00
|
|
|
* - break : corresponds to "break"
|
|
|
|
* - callfunc : a function call with unknown side effects
|
|
|
|
* - continue : corresponds to "continue"
|
|
|
|
* - dealloc : the variable is deallocated
|
|
|
|
* - goto : corresponds to a "goto"
|
2009-07-17 10:49:01 +02:00
|
|
|
* - if : there is an "if"
|
|
|
|
* - if(var) : corresponds with "if ( var != 0 )"
|
|
|
|
* - if(!var) : corresponds with "if ( var == 0 )"
|
|
|
|
* - ifv : the variable is used in some way in a "if"
|
|
|
|
* - loop : corresponds to either a "for" or a "while"
|
2010-03-13 21:12:18 +01:00
|
|
|
* - realloc : the variable is reallocated
|
2009-07-17 10:49:01 +02:00
|
|
|
* - return : corresponds to a "return"
|
2010-03-13 21:12:18 +01:00
|
|
|
* - use : unknown usage -> bail out checking of this execution path
|
|
|
|
* - &use : the address of the variable is taken
|
|
|
|
* - ::use : calling member function of class
|
2008-12-18 22:28:57 +01:00
|
|
|
*/
|
2010-05-16 19:55:16 +02:00
|
|
|
Token *getcode(const Token *tok, std::list<const Token *> callstack, const unsigned int varid, AllocType &alloctype, AllocType &dealloctype, bool classmember, unsigned int sz);
|
2009-02-04 07:11:36 +01:00
|
|
|
|
|
|
|
/**
|
2009-06-08 20:20:43 +02:00
|
|
|
* Simplify code e.g. by replacing empty "{ }" with ";"
|
|
|
|
* @param tok first token. The tokens list can be modified.
|
2009-02-04 07:11:36 +01:00
|
|
|
*/
|
2010-05-16 19:55:16 +02:00
|
|
|
void simplifycode(Token *tok);
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2010-05-16 19:55:16 +02:00
|
|
|
static const Token *findleak(const Token *tokens);
|
2009-08-23 15:48:25 +02:00
|
|
|
|
2009-07-17 10:49:01 +02:00
|
|
|
/**
|
|
|
|
* Checking the variable varname
|
|
|
|
* @param Tok1 start token
|
2009-10-15 18:52:29 +02:00
|
|
|
* @param varname name of variable (for error messages)
|
|
|
|
* @param varid variable id
|
2009-07-17 10:49:01 +02:00
|
|
|
* @param classmember is the scope inside a class member function
|
|
|
|
* @param sz size of type.. if the variable is a "int *" then sz should be "sizeof(int)"
|
|
|
|
*/
|
2009-09-10 21:22:57 +02:00
|
|
|
void checkScope(const Token *Tok1, const std::string &varname, unsigned int varid, bool classmember, unsigned int sz);
|
2008-12-20 18:52:15 +01:00
|
|
|
|
2010-03-13 21:12:18 +01:00
|
|
|
/** Report all possible errors (for the --errorlist) */
|
2010-12-29 12:43:29 +01:00
|
|
|
void getErrorMessages(ErrorLogger *e, const Settings *settings)
|
2009-08-04 21:36:55 +02:00
|
|
|
{
|
2010-12-29 12:43:29 +01:00
|
|
|
CheckMemoryLeakInFunction c(0, settings, e);
|
2009-08-04 21:36:55 +02:00
|
|
|
|
2010-12-29 12:43:29 +01:00
|
|
|
c.memleakError(0, "varname");
|
|
|
|
c.resourceLeakError(0, "varname");
|
|
|
|
|
|
|
|
c.deallocDeallocError(0, "varname");
|
|
|
|
c.deallocuseError(0, "varname");
|
|
|
|
c.mismatchSizeError(0, "sz");
|
2009-08-04 21:36:55 +02:00
|
|
|
std::list<const Token *> callstack;
|
2010-12-29 12:43:29 +01:00
|
|
|
c.mismatchAllocDealloc(callstack, "varname");
|
|
|
|
c.memleakUponReallocFailureError(0, "varname");
|
2009-08-04 21:36:55 +02:00
|
|
|
}
|
2009-06-08 20:20:43 +02:00
|
|
|
|
2010-03-13 21:12:18 +01:00
|
|
|
/**
|
|
|
|
* Get name of class (--doc)
|
|
|
|
* @return name of class
|
|
|
|
*/
|
2011-02-02 10:29:10 +01:00
|
|
|
std::string myName() const
|
2009-06-12 15:20:08 +02:00
|
|
|
{
|
|
|
|
return "Memory leaks (function variables)";
|
|
|
|
}
|
|
|
|
|
2010-03-13 21:12:18 +01:00
|
|
|
/**
|
|
|
|
* Get class information (--doc)
|
|
|
|
* @return Wiki formatted information about this class
|
|
|
|
*/
|
2009-06-12 12:19:37 +02:00
|
|
|
std::string classInfo() const
|
|
|
|
{
|
2009-06-12 15:20:08 +02:00
|
|
|
return "Is there any allocated memory when a function goes out of scope";
|
2009-06-12 12:19:37 +02:00
|
|
|
}
|
2009-11-14 09:06:28 +01:00
|
|
|
|
2010-03-13 21:12:18 +01:00
|
|
|
/** parse tokens to see what functions are "noreturn" */
|
2009-11-14 09:06:28 +01:00
|
|
|
void parse_noreturn();
|
2010-03-13 21:12:18 +01:00
|
|
|
|
|
|
|
/** Function names for functions that are "noreturn" */
|
2009-11-14 09:06:28 +01:00
|
|
|
std::set<std::string> noreturn;
|
2010-12-08 07:49:01 +01:00
|
|
|
|
2011-01-11 20:14:15 +01:00
|
|
|
/** Function names for functions that are not "noreturn" */
|
|
|
|
std::set<std::string> notnoreturn;
|
|
|
|
|
2011-01-16 18:13:54 +01:00
|
|
|
const SymbolDatabase *symbolDatabase;
|
2009-06-08 20:20:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-06-08 20:54:49 +02:00
|
|
|
/**
|
2009-07-17 10:49:01 +02:00
|
|
|
* @brief %Check class variables, variables that are allocated in the constructor should be deallocated in the destructor
|
2009-06-08 20:54:49 +02:00
|
|
|
*/
|
2009-06-08 20:20:43 +02:00
|
|
|
|
2009-07-17 10:49:01 +02:00
|
|
|
class CheckMemoryLeakInClass : private Check, private CheckMemoryLeak
|
2009-06-08 20:20:43 +02:00
|
|
|
{
|
|
|
|
public:
|
2011-02-02 10:29:10 +01:00
|
|
|
CheckMemoryLeakInClass() : Check(myName()), CheckMemoryLeak(0, 0)
|
2009-06-08 20:20:43 +02:00
|
|
|
{ }
|
|
|
|
|
2010-04-08 22:56:34 +02:00
|
|
|
CheckMemoryLeakInClass(const Tokenizer *tokenizr, const Settings *settings, ErrorLogger *errLog)
|
2011-02-02 10:29:10 +01:00
|
|
|
: Check(myName(), tokenizr, settings, errLog), CheckMemoryLeak(tokenizr, errLog)
|
2009-06-08 20:20:43 +02:00
|
|
|
{ }
|
|
|
|
|
2010-04-08 22:56:34 +02:00
|
|
|
void runSimplifiedChecks(const Tokenizer *tokenizr, const Settings *settings, ErrorLogger *errLog)
|
2009-06-08 20:20:43 +02:00
|
|
|
{
|
2010-10-19 21:54:15 +02:00
|
|
|
// Don't use these check for Java and C# programs..
|
|
|
|
if (tokenizr->getFiles()->at(0).find(".java") != std::string::npos ||
|
|
|
|
tokenizr->getFiles()->at(0).find(".cs") != std::string::npos)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-04-08 22:56:34 +02:00
|
|
|
CheckMemoryLeakInClass checkMemoryLeak(tokenizr, settings, errLog);
|
2009-06-08 20:20:43 +02:00
|
|
|
checkMemoryLeak.check();
|
|
|
|
}
|
|
|
|
|
|
|
|
void check();
|
|
|
|
|
|
|
|
private:
|
2011-01-17 18:29:19 +01:00
|
|
|
void variable(const Scope *scope, const Token *tokVarname);
|
2009-06-08 20:20:43 +02:00
|
|
|
|
2010-05-15 19:40:32 +02:00
|
|
|
/** Public functions: possible double-allocation */
|
2011-01-17 18:29:19 +01:00
|
|
|
void checkPublicFunctions(const Scope *scope, const Token *classtok);
|
2010-05-15 19:40:32 +02:00
|
|
|
void publicAllocationError(const Token *tok, const std::string &varname);
|
|
|
|
|
2010-12-29 12:43:29 +01:00
|
|
|
void getErrorMessages(ErrorLogger * /*errorLogger*/, const Settings * /*settings*/)
|
2009-06-08 20:20:43 +02:00
|
|
|
{ }
|
2009-03-22 08:20:15 +01:00
|
|
|
|
2011-02-02 10:29:10 +01:00
|
|
|
std::string myName() const
|
2009-06-12 15:20:08 +02:00
|
|
|
{
|
|
|
|
return "Memory leaks (class variables)";
|
|
|
|
}
|
|
|
|
|
2009-06-12 12:19:37 +02:00
|
|
|
std::string classInfo() const
|
|
|
|
{
|
2009-06-12 15:20:08 +02:00
|
|
|
return "If the constructor allocate memory then the destructor must deallocate it.";
|
2009-06-12 12:19:37 +02:00
|
|
|
}
|
2008-12-18 22:28:57 +01:00
|
|
|
};
|
|
|
|
|
2009-06-08 20:20:43 +02:00
|
|
|
|
2009-06-16 22:01:04 +02:00
|
|
|
|
2009-07-17 10:49:01 +02:00
|
|
|
/** @brief detect simple memory leaks for struct members */
|
2009-06-16 22:01:04 +02:00
|
|
|
|
2009-07-17 10:49:01 +02:00
|
|
|
class CheckMemoryLeakStructMember : private Check, private CheckMemoryLeak
|
2009-06-16 22:01:04 +02:00
|
|
|
{
|
|
|
|
public:
|
2011-02-02 10:29:10 +01:00
|
|
|
CheckMemoryLeakStructMember() : Check(myName()), CheckMemoryLeak(0, 0)
|
2009-06-16 22:01:04 +02:00
|
|
|
{ }
|
|
|
|
|
2010-04-08 22:56:34 +02:00
|
|
|
CheckMemoryLeakStructMember(const Tokenizer *tokenizr, const Settings *settings, ErrorLogger *errLog)
|
2011-02-02 10:29:10 +01:00
|
|
|
: Check(myName(), tokenizr, settings, errLog), CheckMemoryLeak(tokenizr, errLog)
|
2009-06-16 22:01:04 +02:00
|
|
|
{ }
|
|
|
|
|
2010-04-08 22:56:34 +02:00
|
|
|
void runSimplifiedChecks(const Tokenizer *tokenizr, const Settings *settings, ErrorLogger *errLog)
|
2009-06-16 22:01:04 +02:00
|
|
|
{
|
2010-04-08 22:56:34 +02:00
|
|
|
CheckMemoryLeakStructMember checkMemoryLeak(tokenizr, settings, errLog);
|
2009-06-16 22:01:04 +02:00
|
|
|
checkMemoryLeak.check();
|
|
|
|
}
|
|
|
|
|
2009-07-19 16:51:31 +02:00
|
|
|
void check();
|
2009-06-16 22:01:04 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2010-12-29 12:43:29 +01:00
|
|
|
void getErrorMessages(ErrorLogger * /*errorLogger*/, const Settings * /*settings*/)
|
2009-06-16 22:01:04 +02:00
|
|
|
{ }
|
|
|
|
|
2011-02-02 10:29:10 +01:00
|
|
|
std::string myName() const
|
2009-06-16 22:01:04 +02:00
|
|
|
{
|
|
|
|
return "Memory leaks (struct members)";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string classInfo() const
|
|
|
|
{
|
2009-07-17 10:49:01 +02:00
|
|
|
return "Don't forget to deallocate struct members";
|
2009-06-16 22:01:04 +02:00
|
|
|
}
|
|
|
|
};
|
2010-11-12 21:09:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @brief detect simple memory leaks (address not taken) */
|
|
|
|
|
|
|
|
class CheckMemoryLeakNoVar : private Check, private CheckMemoryLeak
|
|
|
|
{
|
|
|
|
public:
|
2011-02-02 10:29:10 +01:00
|
|
|
CheckMemoryLeakNoVar() : Check(myName()), CheckMemoryLeak(0, 0)
|
2010-11-12 21:09:34 +01:00
|
|
|
{ }
|
|
|
|
|
|
|
|
CheckMemoryLeakNoVar(const Tokenizer *tokenizr, const Settings *settings, ErrorLogger *errLog)
|
2011-02-02 10:29:10 +01:00
|
|
|
: Check(myName(), tokenizr, settings, errLog), CheckMemoryLeak(tokenizr, errLog)
|
2010-11-12 21:09:34 +01:00
|
|
|
{ }
|
|
|
|
|
|
|
|
void runSimplifiedChecks(const Tokenizer *tokenizr, const Settings *settings, ErrorLogger *errLog)
|
|
|
|
{
|
|
|
|
CheckMemoryLeakNoVar checkMemoryLeak(tokenizr, settings, errLog);
|
|
|
|
checkMemoryLeak.check();
|
|
|
|
}
|
|
|
|
|
|
|
|
void check();
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
void functionCallLeak(const Token *loc, const std::string &alloc, const std::string &functionCall);
|
|
|
|
|
2010-12-29 12:43:29 +01:00
|
|
|
void getErrorMessages(ErrorLogger * /*errorLogger*/, const Settings * /*settings*/)
|
2010-11-12 21:09:34 +01:00
|
|
|
{ }
|
|
|
|
|
2011-02-02 10:29:10 +01:00
|
|
|
std::string myName() const
|
2010-11-12 21:09:34 +01:00
|
|
|
{
|
|
|
|
return "Memory leaks (address not taken)";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string classInfo() const
|
|
|
|
{
|
|
|
|
return "Not taking the address to allocated memory";
|
|
|
|
}
|
|
|
|
};
|
2009-07-17 10:49:01 +02:00
|
|
|
/// @}
|
2008-12-18 22:28:57 +01:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#endif
|