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
|
2020-05-10 11:16:32 +02:00
|
|
|
* Copyright (C) 2007-2020 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
|
|
|
*
|
2011-09-08 18:23:25 +02:00
|
|
|
* The checking is split up into three 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"
|
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
|
|
|
|
2012-05-03 10:43:47 +02:00
|
|
|
class Function;
|
2017-05-27 04:33:47 +02:00
|
|
|
class Scope;
|
|
|
|
class Settings;
|
|
|
|
class Token;
|
2012-05-03 10:43:47 +02:00
|
|
|
class Variable;
|
2009-03-20 18:16:21 +01:00
|
|
|
|
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 */
|
2012-06-10 14:19:09 +02:00
|
|
|
class CPPCHECKLIB CheckMemoryLeak {
|
2018-06-17 23:09:41 +02:00
|
|
|
private:
|
2010-03-13 21:12:18 +01:00
|
|
|
/** For access to the tokens */
|
2018-06-17 23:09:41 +02:00
|
|
|
const Tokenizer * const mTokenizer_;
|
2010-03-13 21:12:18 +01:00
|
|
|
|
|
|
|
/** ErrorLogger used to report errors */
|
2018-06-17 23:09:41 +02:00
|
|
|
ErrorLogger * const mErrorLogger_;
|
2009-07-13 15:07:26 +02:00
|
|
|
|
2012-03-18 07:49:22 +01:00
|
|
|
/** Enabled standards */
|
2018-06-17 23:09:41 +02:00
|
|
|
const Settings * const mSettings_;
|
2012-03-18 07:49:22 +01:00
|
|
|
|
2009-07-17 10:49:01 +02:00
|
|
|
/**
|
|
|
|
* Report error. Similar with the function Check::reportError
|
2017-04-01 10:34:53 +02:00
|
|
|
* @param tok the token where the error occurs
|
2009-07-17 10:49:01 +02:00
|
|
|
* @param severity the severity of the bug
|
|
|
|
* @param id type of message
|
|
|
|
* @param msg text
|
2015-04-25 17:48:11 +02:00
|
|
|
* @param cwe cwe number
|
2009-07-17 10:49:01 +02:00
|
|
|
*/
|
2017-04-01 10:34:53 +02:00
|
|
|
void reportErr(const Token *tok, Severity::SeverityType severity, const std::string &id, const std::string &msg, const CWE &cwe) 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
|
2015-04-25 17:48:11 +02:00
|
|
|
* @param cwe cwe number
|
2009-07-17 10:49:01 +02:00
|
|
|
*/
|
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;
|
2009-07-13 15:07:26 +02:00
|
|
|
|
2009-06-15 17:44:59 +02:00
|
|
|
public:
|
2018-06-17 19:36:09 +02:00
|
|
|
CheckMemoryLeak() = delete;
|
|
|
|
CheckMemoryLeak(const CheckMemoryLeak &) = delete;
|
|
|
|
void operator=(const CheckMemoryLeak &) = delete;
|
|
|
|
|
2013-07-05 20:55:31 +02:00
|
|
|
CheckMemoryLeak(const Tokenizer *t, ErrorLogger *e, const Settings *s)
|
2018-06-17 23:09:41 +02:00
|
|
|
: mTokenizer_(t), mErrorLogger_(e), mSettings_(s) {
|
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 */
|
2014-01-26 17:02:36 +01:00
|
|
|
enum AllocType { No, Malloc, New, NewArray, File, Fd, Pipe, OtherMem, OtherRes, Many };
|
2009-06-08 20:20:43 +02:00
|
|
|
|
2017-01-07 14:13:22 +01:00
|
|
|
void memoryLeak(const Token *tok, const std::string &varname, AllocType alloctype) const;
|
2009-07-19 16:51:31 +02:00
|
|
|
|
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
|
|
|
|
*/
|
2019-07-17 09:11:42 +02:00
|
|
|
AllocType getDeallocationType(const Token *tok, nonneg 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
|
|
|
*/
|
2019-07-17 09:11:42 +02:00
|
|
|
AllocType getAllocationType(const Token *tok2, nonneg int varid, std::list<const Function*> *callstack = nullptr) 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
|
|
|
*/
|
2019-07-22 10:37:36 +02:00
|
|
|
AllocType getReallocationType(const Token *tok2, nonneg int varid) const;
|
2009-07-19 16:51:31 +02:00
|
|
|
|
2019-08-12 12:53:59 +02:00
|
|
|
/**
|
|
|
|
* Check if token reopens a standard stream
|
|
|
|
* @param tok token to check
|
|
|
|
*/
|
|
|
|
bool isReopenStandardStream(const Token *tok) 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
|
|
|
|
*/
|
2011-12-13 00:24:34 +01:00
|
|
|
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
|
|
|
|
*/
|
2012-05-17 10:33:24 +02:00
|
|
|
void resourceLeakError(const Token *tok, const std::string &varname) const;
|
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
|
|
|
|
*/
|
2011-12-13 00:24:34 +01:00
|
|
|
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;
|
2019-07-22 10:37:36 +02:00
|
|
|
void memleakUponReallocFailureError(const Token *tok, const std::string &reallocfunction, const std::string &varname) const;
|
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? */
|
2016-05-07 16:30:54 +02:00
|
|
|
AllocType functionReturnType(const Function* func, std::list<const Function*> *callstack = nullptr) const;
|
2010-06-05 01:58:50 +02:00
|
|
|
|
|
|
|
/** Function allocates pointed-to argument (a la asprintf)? */
|
2019-07-17 09:11:42 +02:00
|
|
|
const char *functionArgAlloc(const Function *func, nonneg 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
|
|
|
|
2012-06-10 14:19:09 +02:00
|
|
|
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 */
|
2018-06-18 09:40:27 +02:00
|
|
|
CheckMemoryLeakInFunction() : Check(myName()), CheckMemoryLeak(nullptr, nullptr, nullptr) {
|
2013-08-07 16:27:37 +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 */
|
2018-06-18 09:40:27 +02:00
|
|
|
CheckMemoryLeakInFunction(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
|
|
|
|
: Check(myName(), tokenizer, settings, errorLogger), CheckMemoryLeak(tokenizer, errorLogger, settings) {
|
2010-12-08 07:49:01 +01:00
|
|
|
}
|
2009-03-20 18:16:21 +01:00
|
|
|
|
2019-03-16 09:17:50 +01:00
|
|
|
void runChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) OVERRIDE {
|
2018-06-18 09:40:27 +02:00
|
|
|
CheckMemoryLeakInFunction checkMemoryLeak(tokenizer, settings, errorLogger);
|
2010-05-18 07:46:48 +02:00
|
|
|
checkMemoryLeak.checkReallocUsage();
|
2009-03-20 18:16:21 +01:00
|
|
|
}
|
|
|
|
|
2010-03-13 21:12:18 +01:00
|
|
|
/** @brief Unit testing : testing the white list */
|
2015-01-30 20:55:53 +01:00
|
|
|
static bool test_white_list(const std::string &funcname, const Settings *settings, bool cpp);
|
2010-01-18 21:34:11 +01:00
|
|
|
|
2010-05-18 07:46:48 +02:00
|
|
|
/**
|
|
|
|
* Checking for a memory leak caused by improper realloc usage.
|
|
|
|
*/
|
|
|
|
void checkReallocUsage();
|
|
|
|
|
2012-03-16 19:52:18 +01:00
|
|
|
private:
|
2010-03-13 21:12:18 +01:00
|
|
|
/** Report all possible errors (for the --errorlist) */
|
2019-01-12 07:37:42 +01:00
|
|
|
void getErrorMessages(ErrorLogger *e, const Settings *settings) const OVERRIDE {
|
2016-05-07 16:30:54 +02:00
|
|
|
CheckMemoryLeakInFunction c(nullptr, settings, e);
|
2009-08-04 21:36:55 +02:00
|
|
|
|
2016-05-07 16:30:54 +02:00
|
|
|
c.memleakError(nullptr, "varname");
|
|
|
|
c.resourceLeakError(nullptr, "varname");
|
2010-12-29 12:43:29 +01:00
|
|
|
|
2016-05-07 16:30:54 +02:00
|
|
|
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;
|
2010-12-29 12:43:29 +01:00
|
|
|
c.mismatchAllocDealloc(callstack, "varname");
|
2019-07-22 10:37:36 +02:00
|
|
|
c.memleakUponReallocFailureError(nullptr, "realloc", "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
|
|
|
|
*/
|
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
|
|
|
|
*/
|
2019-01-12 07:37:42 +01:00
|
|
|
std::string classInfo() const OVERRIDE {
|
2012-08-26 16:22:46 +02:00
|
|
|
return "Is there any allocated memory when a function goes out of scope\n";
|
2009-06-12 12:19:37 +02:00
|
|
|
}
|
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
|
|
|
|
2012-06-10 14:19:09 +02:00
|
|
|
class CPPCHECKLIB CheckMemoryLeakInClass : private Check, private CheckMemoryLeak {
|
2009-06-08 20:20:43 +02:00
|
|
|
public:
|
2017-08-09 20:00:26 +02:00
|
|
|
CheckMemoryLeakInClass() : Check(myName()), CheckMemoryLeak(nullptr, nullptr, nullptr) {
|
2013-08-07 16:27:37 +02:00
|
|
|
}
|
2009-06-08 20:20:43 +02:00
|
|
|
|
2018-06-18 09:40:27 +02:00
|
|
|
CheckMemoryLeakInClass(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
|
|
|
|
: Check(myName(), tokenizer, settings, errorLogger), CheckMemoryLeak(tokenizer, errorLogger, settings) {
|
2013-08-07 16:27:37 +02:00
|
|
|
}
|
2009-06-08 20:20:43 +02:00
|
|
|
|
2019-03-16 09:17:50 +01:00
|
|
|
void runChecks(const Tokenizer *tokenizr, const Settings *settings, ErrorLogger *errLog) OVERRIDE {
|
2012-09-10 19:02:32 +02:00
|
|
|
if (!tokenizr->isCPP())
|
2010-10-19 21:54:15 +02:00
|
|
|
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);
|
|
|
|
|
2012-09-10 17:27:41 +02:00
|
|
|
void unsafeClassError(const Token *tok, const std::string &classname, const std::string &varname);
|
|
|
|
|
2019-01-12 07:37:42 +01:00
|
|
|
void getErrorMessages(ErrorLogger *e, const Settings *settings) const OVERRIDE {
|
2017-08-09 20:00:26 +02:00
|
|
|
CheckMemoryLeakInClass c(nullptr, settings, e);
|
|
|
|
c.publicAllocationError(nullptr, "varname");
|
|
|
|
c.unsafeClassError(nullptr, "class", "class::varname");
|
2012-09-10 17:27:41 +02:00
|
|
|
}
|
2009-03-22 08:20:15 +01:00
|
|
|
|
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)";
|
|
|
|
}
|
|
|
|
|
2019-01-12 07:37:42 +01:00
|
|
|
std::string classInfo() const OVERRIDE {
|
2012-08-26 16:22:46 +02:00
|
|
|
return "If the constructor allocate memory then the destructor must deallocate it.\n";
|
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
|
|
|
|
2012-06-10 14:19:09 +02:00
|
|
|
class CPPCHECKLIB CheckMemoryLeakStructMember : private Check, private CheckMemoryLeak {
|
2009-06-16 22:01:04 +02:00
|
|
|
public:
|
2017-08-09 20:00:26 +02:00
|
|
|
CheckMemoryLeakStructMember() : Check(myName()), CheckMemoryLeak(nullptr, nullptr, nullptr) {
|
2013-08-07 16:27:37 +02:00
|
|
|
}
|
2009-06-16 22:01:04 +02:00
|
|
|
|
2018-06-18 09:40:27 +02:00
|
|
|
CheckMemoryLeakStructMember(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
|
|
|
|
: Check(myName(), tokenizer, settings, errorLogger), CheckMemoryLeak(tokenizer, errorLogger, settings) {
|
2013-08-07 16:27:37 +02:00
|
|
|
}
|
2009-06-16 22:01:04 +02:00
|
|
|
|
2019-03-16 09:17:50 +01:00
|
|
|
void runChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) OVERRIDE {
|
2018-06-18 09:40:27 +02:00
|
|
|
CheckMemoryLeakStructMember checkMemoryLeak(tokenizer, settings, errorLogger);
|
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:
|
|
|
|
|
2011-05-11 18:19:14 +02:00
|
|
|
/** Is local variable allocated with malloc? */
|
2012-05-03 10:43:47 +02:00
|
|
|
static bool isMalloc(const Variable *variable);
|
2011-05-11 18:19:14 +02:00
|
|
|
|
2012-05-03 10:43:47 +02:00
|
|
|
void checkStructVariable(const Variable * const variable);
|
2011-05-11 18:19:14 +02:00
|
|
|
|
2019-01-12 07:37:42 +01:00
|
|
|
void getErrorMessages(ErrorLogger * /*errorLogger*/, const Settings * /*settings*/) const OVERRIDE {
|
2013-08-07 16:27:37 +02:00
|
|
|
}
|
2009-06-16 22:01:04 +02:00
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
static std::string myName() {
|
2009-06-16 22:01:04 +02:00
|
|
|
return "Memory leaks (struct members)";
|
|
|
|
}
|
|
|
|
|
2019-01-12 07:37:42 +01:00
|
|
|
std::string classInfo() const OVERRIDE {
|
2012-08-26 16:22:46 +02:00
|
|
|
return "Don't forget to deallocate struct members\n";
|
2009-06-16 22:01:04 +02:00
|
|
|
}
|
|
|
|
};
|
2010-11-12 21:09:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @brief detect simple memory leaks (address not taken) */
|
|
|
|
|
2012-06-10 14:19:09 +02:00
|
|
|
class CPPCHECKLIB CheckMemoryLeakNoVar : private Check, private CheckMemoryLeak {
|
2010-11-12 21:09:34 +01:00
|
|
|
public:
|
2017-08-09 20:00:26 +02:00
|
|
|
CheckMemoryLeakNoVar() : Check(myName()), CheckMemoryLeak(nullptr, nullptr, nullptr) {
|
2013-08-07 16:27:37 +02:00
|
|
|
}
|
2010-11-12 21:09:34 +01:00
|
|
|
|
2018-06-18 09:40:27 +02:00
|
|
|
CheckMemoryLeakNoVar(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
|
|
|
|
: Check(myName(), tokenizer, settings, errorLogger), CheckMemoryLeak(tokenizer, errorLogger, settings) {
|
2013-08-07 16:27:37 +02:00
|
|
|
}
|
2010-11-12 21:09:34 +01:00
|
|
|
|
2019-03-16 09:17:50 +01:00
|
|
|
void runChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) OVERRIDE {
|
2018-06-18 09:40:27 +02:00
|
|
|
CheckMemoryLeakNoVar checkMemoryLeak(tokenizer, settings, errorLogger);
|
2010-11-12 21:09:34 +01:00
|
|
|
checkMemoryLeak.check();
|
|
|
|
}
|
|
|
|
|
|
|
|
void check();
|
|
|
|
|
2012-03-15 18:52:51 +01:00
|
|
|
private:
|
2019-08-13 13:00:59 +02:00
|
|
|
/**
|
|
|
|
* @brief %Check if an input argument to a function is the return value of an allocation function
|
|
|
|
* like malloc(), and the function does not release it.
|
|
|
|
* @param scope The scope of the function to check.
|
|
|
|
*/
|
|
|
|
void checkForUnreleasedInputArgument(const Scope *scope);
|
|
|
|
|
2014-01-03 10:12:32 +01:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
2015-01-04 11:07:53 +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);
|
2014-01-03 10:12:32 +01:00
|
|
|
void returnValueNotUsedError(const Token* tok, const std::string &alloc);
|
2015-01-04 11:07:53 +01:00
|
|
|
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
|
|
|
|
2019-01-12 07:37:42 +01:00
|
|
|
void getErrorMessages(ErrorLogger *e, const Settings *settings) const OVERRIDE {
|
2016-05-07 16:30:54 +02:00
|
|
|
CheckMemoryLeakNoVar c(nullptr, settings, e);
|
2012-03-14 23:44:04 +01:00
|
|
|
|
2016-05-07 16:30:54 +02:00
|
|
|
c.functionCallLeak(nullptr, "funcName", "funcName");
|
|
|
|
c.returnValueNotUsedError(nullptr, "funcName");
|
|
|
|
c.unsafeArgAllocError(nullptr, "funcName", "shared_ptr", "int");
|
2012-03-14 23:44:04 +01:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
static std::string myName() {
|
2010-11-12 21:09:34 +01:00
|
|
|
return "Memory leaks (address not taken)";
|
|
|
|
}
|
|
|
|
|
2019-01-12 07:37:42 +01:00
|
|
|
std::string classInfo() const OVERRIDE {
|
2012-08-26 16:22:46 +02:00
|
|
|
return "Not taking the address to allocated memory\n";
|
2010-11-12 21:09:34 +01:00
|
|
|
}
|
|
|
|
};
|
2009-07-17 10:49:01 +02:00
|
|
|
/// @}
|
2008-12-18 22:28:57 +01:00
|
|
|
//---------------------------------------------------------------------------
|
2013-09-04 20:59:49 +02:00
|
|
|
#endif // checkmemoryleakH
|