cppcheck/lib/checkmemoryleak.h

468 lines
17 KiB
C
Raw Normal View History

2008-12-18 22:28:57 +01:00
/*
* Cppcheck - A tool for static C/C++ code analysis
2018-01-14 15:37:52 +01:00
* Copyright (C) 2007-2017 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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2008-12-18 22:28:57 +01:00
*/
//---------------------------------------------------------------------------
#ifndef checkmemoryleakH
#define checkmemoryleakH
2008-12-18 22:28:57 +01:00
//---------------------------------------------------------------------------
/**
2010-03-13 22:16:06 +01:00
* @file
*
* %Check for memory leaks
*
2011-09-08 18:23:25 +02:00
* The checking is split up into three specialized classes.
* - 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
*/
2008-12-18 22:28:57 +01:00
2009-03-20 18:16:21 +01:00
#include "check.h"
2017-05-27 04:33:47 +02:00
#include "config.h"
#include "errorlogger.h"
#include "tokenize.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
class Function;
2017-05-27 04:33:47 +02:00
class Scope;
class Settings;
class SymbolDatabase;
class Token;
class Variable;
2009-03-20 18:16:21 +01:00
2010-03-13 21:12:18 +01:00
/// @addtogroup Core
/// @{
/** @brief Base class for memory leaks checking */
class CPPCHECKLIB CheckMemoryLeak {
protected:
2010-03-13 21:12:18 +01:00
/** For access to the tokens */
const Tokenizer * const tokenizer;
2010-03-13 21:12:18 +01:00
private:
2010-03-13 21:12:18 +01:00
/** ErrorLogger used to report errors */
ErrorLogger * const errorLogger;
/** Enabled standards */
2013-07-05 20:55:31 +02:00
const Settings * const settings1;
/** Disable the default constructors */
CheckMemoryLeak();
/** Disable the default constructors */
CheckMemoryLeak(const CheckMemoryLeak &);
/** disable assignment operator */
void operator=(const CheckMemoryLeak &);
/**
* Report error. Similar with the function Check::reportError
* @param tok the token where the error occurs
* @param severity the severity of the bug
* @param id type of message
* @param msg text
* @param cwe cwe number
*/
void reportErr(const Token *tok, Severity::SeverityType severity, const std::string &id, const std::string &msg, const CWE &cwe) const;
/**
* 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
* @param cwe cwe number
*/
2016-02-27 16:03:50 +01:00
void reportErr(const std::list<const Token *> &callstack, Severity::SeverityType severity, const std::string &id, const std::string &msg, const CWE &cwe) const;
public:
2013-07-05 20:55:31 +02:00
CheckMemoryLeak(const Tokenizer *t, ErrorLogger *e, const Settings *s)
2014-11-20 14:20:09 +01:00
: tokenizer(t), errorLogger(e), settings1(s) {
}
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 */
enum AllocType { No, Malloc, New, NewArray, File, Fd, Pipe, OtherMem, OtherRes, Many };
void memoryLeak(const Token *tok, const std::string &varname, AllocType alloctype) const;
/**
2010-03-13 21:12:18 +01:00
* @brief Get type of deallocation at given position
* @param tok position
2009-10-15 18:52:29 +02:00
* @param varid variable id
* @return type of deallocation
*/
AllocType getDeallocationType(const Token *tok, unsigned int varid) const;
/**
2010-03-13 21:12:18 +01:00
* @brief Get type of allocation at given position
*/
AllocType getAllocationType(const Token *tok2, unsigned int varid, std::list<const Function*> *callstack = nullptr) const;
/**
2010-03-13 21:12:18 +01:00
* @brief Get type of reallocation at given position
*/
static AllocType getReallocationType(const Token *tok2, unsigned int varid);
2010-03-13 21:12:18 +01:00
/**
* @brief Is a typename the name of a class?
2011-09-08 18:23:25 +02:00
* @param tok type token
* @param varid variable id
2010-03-13 21:12:18 +01:00
* @return true if the type name is the name of a class
*/
bool isclass(const Token *tok, unsigned int varid) const;
2011-09-08 18:23:25 +02:00
/**
* Report that there is a memory leak (new/malloc/etc)
* @param tok token where memory is leaked
* @param varname name of variable
*/
void memleakError(const Token *tok, const std::string &varname) const;
2011-09-08 18:23:25 +02:00
/**
* Report that there is a resource leak (fopen/popen/etc)
* @param tok token where resource is leaked
* @param varname name of variable
*/
void resourceLeakError(const Token *tok, const std::string &varname) const;
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
*/
void deallocDeallocError(const Token *tok, const std::string &varname) const;
void deallocuseError(const Token *tok, const std::string &varname) const;
void mismatchSizeError(const Token *tok, const std::string &sz) const;
void mismatchAllocDealloc(const std::list<const Token *> &callstack, const std::string &varname) const;
void memleakUponReallocFailureError(const Token *tok, const std::string &varname) const;
/** What type of allocated memory does the given function return? */
AllocType functionReturnType(const Function* func, std::list<const Function*> *callstack = nullptr) const;
/** Function allocates pointed-to argument (a la asprintf)? */
const char *functionArgAlloc(const Function *func, unsigned int targetpar, AllocType &allocType) const;
};
2010-03-13 21:12:18 +01:00
/// @}
/// @addtogroup Checks
/// @{
/**
2010-03-13 22:16:06 +01:00
* @brief %CheckMemoryLeakInFunction detects when a function variable is allocated but not deallocated properly.
*
* The checking is done by looking at each function variable separately. By repeating these 4 steps over and over:
* -# 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.
*/
class CPPCHECKLIB 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 */
CheckMemoryLeakInFunction() : Check(myName()), CheckMemoryLeak(nullptr, nullptr, nullptr), symbolDatabase(nullptr) {
}
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)
2014-11-20 14:20:09 +01:00
: Check(myName(), tokenizr, settings, errLog), CheckMemoryLeak(tokenizr, errLog, settings) {
// get the symbol database
if (tokenizr)
symbolDatabase = tokenizr->getSymbolDatabase();
else
symbolDatabase = nullptr;
}
2009-03-20 18:16:21 +01:00
2010-03-13 21:12:18 +01:00
/** @brief run all simplified checks */
2018-05-15 16:37:40 +02:00
void runSimplifiedChecks(const Tokenizer *tokenizr, const Settings *settings, ErrorLogger *errLog) override {
2010-04-08 22:56:34 +02:00
CheckMemoryLeakInFunction checkMemoryLeak(tokenizr, settings, errLog);
checkMemoryLeak.checkReallocUsage();
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 */
static bool test_white_list(const std::string &funcname, const Settings *settings, bool cpp);
2010-03-13 21:12:18 +01:00
/** @brief Perform checking */
void check();
2008-12-18 22:28:57 +01:00
/**
* Checking for a memory leak caused by improper realloc usage.
*/
void checkReallocUsage();
/**
* 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
* @param alloctype if memory is allocated, this indicates the type of allocation
* @param dealloctype if memory is deallocated, this indicates the type of deallocation
* @param allocpar if function allocates varid parameter
* @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
* - "&use"
*/
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
/**
* 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
* @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 TokenList::deleteTokens(returnValue);
* Returned tokens:
2011-09-08 18:28:05 +02:00
* - "alloc" : the variable is allocated
* - "assign" : the variable is assigned a new value
* - "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"
* - "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"
* - "realloc" : the variable is reallocated
* - "return" : corresponds to a "return"
* - "use" : unknown usage -> bail out checking of this execution path
* - "&use" : the address of the variable is taken
* - "::use" : calling member function of class
* - "use_" : content of variable is accessed (used to warn access after dealloc)
2008-12-18 22:28:57 +01: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
/**
* 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
*/
void simplifycode(Token *tok) const;
2008-12-18 22:28:57 +01:00
static const Token *findleak(const Token *tokens);
/**
* Checking the variable varname
2015-04-01 15:39:45 +02:00
* @param startTok start token
2009-10-15 18:52:29 +02:00
* @param varname name of variable (for error messages)
* @param varid variable id
* @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)"
*/
2015-04-01 15:39:45 +02:00
void checkScope(const Token *startTok, const std::string &varname, unsigned int varid, bool classmember, unsigned int sz);
private:
2010-03-13 21:12:18 +01:00
/** Report all possible errors (for the --errorlist) */
2018-05-15 16:37:40 +02:00
void getErrorMessages(ErrorLogger *e, const Settings *settings) const override {
CheckMemoryLeakInFunction c(nullptr, settings, e);
2009-08-04 21:36:55 +02:00
c.memleakError(nullptr, "varname");
c.resourceLeakError(nullptr, "varname");
c.deallocDeallocError(nullptr, "varname");
c.deallocuseError(nullptr, "varname");
c.mismatchSizeError(nullptr, "sz");
2018-04-04 21:51:31 +02:00
const std::list<const Token *> callstack;
c.mismatchAllocDealloc(callstack, "varname");
c.memleakUponReallocFailureError(nullptr, "varname");
2009-08-04 21:36:55 +02:00
}
2010-03-13 21:12:18 +01:00
/**
* Get name of class (--doc)
* @return name of class
*/
2014-11-20 14:20:09 +01:00
static std::string myName() {
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
*/
2018-05-15 16:37:40 +02:00
std::string classInfo() const override {
return "Is there any allocated memory when a function goes out of scope\n";
}
const SymbolDatabase *symbolDatabase;
};
/**
* @brief %Check class variables, variables that are allocated in the constructor should be deallocated in the destructor
*/
class CPPCHECKLIB CheckMemoryLeakInClass : private Check, private CheckMemoryLeak {
public:
CheckMemoryLeakInClass() : Check(myName()), CheckMemoryLeak(nullptr, nullptr, nullptr) {
}
2010-04-08 22:56:34 +02:00
CheckMemoryLeakInClass(const Tokenizer *tokenizr, const Settings *settings, ErrorLogger *errLog)
2014-11-20 14:20:09 +01:00
: Check(myName(), tokenizr, settings, errLog), CheckMemoryLeak(tokenizr, errLog, settings) {
}
2018-05-15 16:37:40 +02:00
void runSimplifiedChecks(const Tokenizer *tokenizr, const Settings *settings, ErrorLogger *errLog) override {
if (!tokenizr->isCPP())
return;
2010-04-08 22:56:34 +02:00
CheckMemoryLeakInClass checkMemoryLeak(tokenizr, settings, errLog);
checkMemoryLeak.check();
}
void check();
private:
void variable(const Scope *scope, const Token *tokVarname);
/** Public functions: possible double-allocation */
void checkPublicFunctions(const Scope *scope, const Token *classtok);
void publicAllocationError(const Token *tok, const std::string &varname);
void unsafeClassError(const Token *tok, const std::string &classname, const std::string &varname);
2018-05-15 16:37:40 +02:00
void getErrorMessages(ErrorLogger *e, const Settings *settings) const override {
CheckMemoryLeakInClass c(nullptr, settings, e);
c.publicAllocationError(nullptr, "varname");
c.unsafeClassError(nullptr, "class", "class::varname");
}
2014-11-20 14:20:09 +01:00
static std::string myName() {
2009-06-12 15:20:08 +02:00
return "Memory leaks (class variables)";
}
2018-05-15 16:37:40 +02:00
std::string classInfo() const override {
return "If the constructor allocate memory then the destructor must deallocate it.\n";
}
2008-12-18 22:28:57 +01:00
};
/** @brief detect simple memory leaks for struct members */
class CPPCHECKLIB CheckMemoryLeakStructMember : private Check, private CheckMemoryLeak {
public:
CheckMemoryLeakStructMember() : Check(myName()), CheckMemoryLeak(nullptr, nullptr, nullptr) {
}
2010-04-08 22:56:34 +02:00
CheckMemoryLeakStructMember(const Tokenizer *tokenizr, const Settings *settings, ErrorLogger *errLog)
2014-11-20 14:20:09 +01:00
: Check(myName(), tokenizr, settings, errLog), CheckMemoryLeak(tokenizr, errLog, settings) {
}
2018-05-15 16:37:40 +02:00
void runSimplifiedChecks(const Tokenizer *tokenizr, const Settings *settings, ErrorLogger *errLog) override {
2010-04-08 22:56:34 +02:00
CheckMemoryLeakStructMember checkMemoryLeak(tokenizr, settings, errLog);
checkMemoryLeak.check();
}
void check();
private:
/** Is local variable allocated with malloc? */
static bool isMalloc(const Variable *variable);
void checkStructVariable(const Variable * const variable);
2018-05-15 16:37:40 +02:00
void getErrorMessages(ErrorLogger * /*errorLogger*/, const Settings * /*settings*/) const override {
}
2014-11-20 14:20:09 +01:00
static std::string myName() {
return "Memory leaks (struct members)";
}
2018-05-15 16:37:40 +02:00
std::string classInfo() const override {
return "Don't forget to deallocate struct members\n";
}
};
/** @brief detect simple memory leaks (address not taken) */
class CPPCHECKLIB CheckMemoryLeakNoVar : private Check, private CheckMemoryLeak {
public:
CheckMemoryLeakNoVar() : Check(myName()), CheckMemoryLeak(nullptr, nullptr, nullptr) {
}
CheckMemoryLeakNoVar(const Tokenizer *tokenizr, const Settings *settings, ErrorLogger *errLog)
2014-11-20 14:20:09 +01:00
: Check(myName(), tokenizr, settings, errLog), CheckMemoryLeak(tokenizr, errLog, settings) {
}
2018-05-15 16:37:40 +02:00
void runSimplifiedChecks(const Tokenizer *tokenizr, const Settings *settings, ErrorLogger *errLog) override {
CheckMemoryLeakNoVar checkMemoryLeak(tokenizr, settings, errLog);
checkMemoryLeak.check();
}
void check();
2012-03-15 18:52:51 +01:00
private:
/**
* @brief %Check if a call to an allocation function like malloc() is made and its return value is not assigned.
* @param scope The scope of the function to check.
*/
void checkForUnusedReturnValue(const Scope *scope);
2012-03-15 18:52:51 +01:00
/**
* @brief %Check if an exception could cause a leak in an argument constructed with shared_ptr/unique_ptr.
* @param scope The scope of the function to check.
*/
void checkForUnsafeArgAlloc(const Scope *scope);
2012-03-15 18:52:51 +01:00
void functionCallLeak(const Token *loc, const std::string &alloc, const std::string &functionCall);
void returnValueNotUsedError(const Token* tok, const std::string &alloc);
void unsafeArgAllocError(const Token *tok, const std::string &funcName, const std::string &ptrType, const std::string &objType);
2012-03-15 18:52:51 +01:00
2018-05-15 16:37:40 +02:00
void getErrorMessages(ErrorLogger *e, const Settings *settings) const override {
CheckMemoryLeakNoVar c(nullptr, settings, e);
c.functionCallLeak(nullptr, "funcName", "funcName");
c.returnValueNotUsedError(nullptr, "funcName");
c.unsafeArgAllocError(nullptr, "funcName", "shared_ptr", "int");
}
2014-11-20 14:20:09 +01:00
static std::string myName() {
return "Memory leaks (address not taken)";
}
2018-05-15 16:37:40 +02:00
std::string classInfo() const override {
return "Not taking the address to allocated memory\n";
}
};
/// @}
2008-12-18 22:28:57 +01:00
//---------------------------------------------------------------------------
#endif // checkmemoryleakH