20 #ifndef checkmemoryleakH 
   21 #define checkmemoryleakH 
   74     void reportErr(
const Token *tok, 
Severity severity, 
const std::string &
id, 
const std::string &msg, 
const CWE &cwe) 
const;
 
   84     void reportErr(
const std::list<const Token *> &callstack, 
Severity severity, 
const std::string &
id, 
const std::string &msg, 
const CWE &cwe) 
const;
 
   92         : mTokenizer_(t), mErrorLogger_(e), mSettings_(s) {}
 
   95     enum AllocType { No, Malloc, New, NewArray, File, Fd, Pipe, OtherMem, OtherRes, Many };
 
   97     void memoryLeak(
const Token *tok, 
const std::string &varname, AllocType alloctype) 
const;
 
  105     AllocType getDeallocationType(
const Token *tok, 
nonneg int varid) 
const;
 
  110     AllocType getAllocationType(
const Token *tok2, 
nonneg int varid, std::list<const Function*> *callstack = 
nullptr) 
const;
 
  115     AllocType getReallocationType(
const Token *tok2, 
nonneg int varid) 
const;
 
  121     bool isReopenStandardStream(
const Token *tok) 
const;
 
  126     bool isOpenDevNull(
const Token *tok) 
const;
 
  132     void memleakError(
const Token *tok, 
const std::string &varname) 
const;
 
  139     void resourceLeakError(
const Token *tok, 
const std::string &varname) 
const;
 
  141     void deallocuseError(
const Token *tok, 
const std::string &varname) 
const;
 
  142     void mismatchAllocDealloc(
const std::list<const Token *> &callstack, 
const std::string &varname) 
const;
 
  143     void memleakUponReallocFailureError(
const Token *tok, 
const std::string &reallocfunction, 
const std::string &varname) 
const;
 
  146     AllocType functionReturnType(
const Function* func, std::list<const Function*> *callstack = 
nullptr) 
const;
 
  168     friend class TestMemleakInFunction;
 
  177         : 
Check(myName(), tokenizer, settings, errorLogger), 
CheckMemoryLeak(tokenizer, errorLogger, settings) {}
 
  187     void checkReallocUsage();
 
  195         const std::list<const Token *> callstack;
 
  205         return "Memory leaks (function variables)";
 
  213         return "Is there any allocated memory when a function goes out of scope\n";
 
  224     friend class TestMemleakInClass;
 
  231         : 
Check(myName(), tokenizer, settings, errorLogger), 
CheckMemoryLeak(tokenizer, errorLogger, settings) {}
 
  234         if (!tokenizer.
isCPP())
 
  238         checkMemoryLeak.
check();
 
  243     void variable(
const Scope *scope, 
const Token *tokVarname);
 
  246     void checkPublicFunctions(
const Scope *scope, 
const Token *classtok);
 
  247     void publicAllocationError(
const Token *tok, 
const std::string &varname);
 
  249     void unsafeClassError(
const Token *tok, 
const std::string &classname, 
const std::string &varname);
 
  258         return "Memory leaks (class variables)";
 
  262         return "If the constructor allocate memory then the destructor must deallocate it.\n";
 
  271     friend class TestMemleakStructMember;
 
  278         : 
Check(myName(), tokenizer, settings, errorLogger), 
CheckMemoryLeak(tokenizer, errorLogger, settings) {}
 
  282         checkMemoryLeak.
check();
 
  288     bool isMalloc(
const Variable *variable) 
const;
 
  290     void checkStructVariable(
const Variable* 
const variable) 
const;
 
  295         return "Memory leaks (struct members)";
 
  299         return "Don't forget to deallocate struct members\n";
 
  308     friend class TestMemleakNoVar;
 
  315         : 
Check(myName(), tokenizer, settings, errorLogger), 
CheckMemoryLeak(tokenizer, errorLogger, settings) {}
 
  319         checkMemoryLeak.
check();
 
  329     void checkForUnreleasedInputArgument(
const Scope *scope);
 
  335     void checkForUnusedReturnValue(
const Scope *scope);
 
  341     void checkForUnsafeArgAlloc(
const Scope *scope);
 
  343     void functionCallLeak(
const Token *loc, 
const std::string &alloc, 
const std::string &functionCall);
 
  344     void returnValueNotUsedError(
const Token* tok, 
const std::string &alloc);
 
  345     void unsafeArgAllocError(
const Token *tok, 
const std::string &funcName, 
const std::string &ptrType, 
const std::string &objType);
 
  355         return "Memory leaks (address not taken)";
 
  359         return "Not taking the address to allocated memory\n";
 
Check class variables, variables that are allocated in the constructor should be deallocated in the d...
 
void getErrorMessages(ErrorLogger *e, const Settings *settings) const override
get error messages
 
std::string classInfo() const override
get information about this class, used to generate documentation
 
void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override
run checks, the token list is not simplified
 
void unsafeClassError(const Token *tok, const std::string &classname, const std::string &varname)
 
static std::string myName()
 
CheckMemoryLeakInClass(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
 
void publicAllocationError(const Token *tok, const std::string &varname)
 
CheckMemoryLeakInFunction detects when a function variable is allocated but not deallocated properly.
 
CheckMemoryLeakInFunction()
This constructor is used when registering this class.
 
static std::string myName()
Get name of class (–doc)
 
void checkReallocUsage()
Checking for a memory leak caused by improper realloc usage.
 
void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override
run checks, the token list is not simplified
 
void getErrorMessages(ErrorLogger *e, const Settings *settings) const override
Report all possible errors (for the –errorlist)
 
std::string classInfo() const override
Get class information (–doc)
 
CheckMemoryLeakInFunction(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
This constructor is used when running checks.
 
detect simple memory leaks (address not taken)
 
void returnValueNotUsedError(const Token *tok, const std::string &alloc)
 
static std::string myName()
 
void functionCallLeak(const Token *loc, const std::string &alloc, const std::string &functionCall)
 
void unsafeArgAllocError(const Token *tok, const std::string &funcName, const std::string &ptrType, const std::string &objType)
 
void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override
run checks, the token list is not simplified
 
std::string classInfo() const override
get information about this class, used to generate documentation
 
CheckMemoryLeakNoVar(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
 
void getErrorMessages(ErrorLogger *e, const Settings *settings) const override
get error messages
 
detect simple memory leaks for struct members
 
static std::string myName()
 
std::string classInfo() const override
get information about this class, used to generate documentation
 
CheckMemoryLeakStructMember(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
 
CheckMemoryLeakStructMember()
 
void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override
run checks, the token list is not simplified
 
void getErrorMessages(ErrorLogger *, const Settings *) const override
get error messages
 
Base class for memory leaks checking.
 
CheckMemoryLeak(const CheckMemoryLeak &)=delete
 
void deallocuseError(const Token *tok, const std::string &varname) const
 
const Settings *const mSettings_
Enabled standards.
 
const Tokenizer *const mTokenizer_
For access to the tokens.
 
void resourceLeakError(const Token *tok, const std::string &varname) const
Report that there is a resource leak (fopen/popen/etc)
 
void mismatchAllocDealloc(const std::list< const Token * > &callstack, const std::string &varname) const
 
ErrorLogger *const mErrorLogger_
ErrorLogger used to report errors.
 
AllocType
What type of allocation are used.
 
CheckMemoryLeak & operator=(const CheckMemoryLeak &)=delete
 
void memleakUponReallocFailureError(const Token *tok, const std::string &reallocfunction, const std::string &varname) const
 
CheckMemoryLeak(const Tokenizer *t, ErrorLogger *e, const Settings *s)
 
void memleakError(const Token *tok, const std::string &varname) const
Report that there is a memory leak (new/malloc/etc)
 
Interface class that cppcheck uses to communicate with the checks.
 
This is an interface, which the class responsible of error logging should implement.
 
This is just a container for general settings so that we don't need to pass individual values to func...
 
The token list that the TokenList generates is a linked-list of this class.
 
The main purpose is to tokenize the source code.
 
const Settings & getSettings() const
 
bool isCPP() const
Is the code CPP.
 
Information about a member variable.
 
Severity
enum class for severity.