Cppcheck
Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
CheckMemoryLeak Class Reference

Base class for memory leaks checking. More...

#include <checkmemoryleak.h>

Inheritance diagram for CheckMemoryLeak:
CheckMemoryLeakInClass CheckMemoryLeakInFunction CheckMemoryLeakNoVar CheckMemoryLeakStructMember

Public Types

enum  AllocType {
  No , Malloc , New , NewArray ,
  File , Fd , Pipe , OtherMem ,
  OtherRes , Many
}
 What type of allocation are used. More...
 

Public Member Functions

 CheckMemoryLeak ()=delete
 
 CheckMemoryLeak (const CheckMemoryLeak &)=delete
 
CheckMemoryLeakoperator= (const CheckMemoryLeak &)=delete
 
 CheckMemoryLeak (const Tokenizer *t, ErrorLogger *e, const Settings *s)
 
void memoryLeak (const Token *tok, const std::string &varname, AllocType alloctype) const
 
AllocType getDeallocationType (const Token *tok, nonneg int varid) const
 Get type of deallocation at given position. More...
 
AllocType getAllocationType (const Token *tok2, nonneg int varid, std::list< const Function * > *callstack=nullptr) const
 Get type of allocation at given position. More...
 
AllocType getReallocationType (const Token *tok2, nonneg int varid) const
 Get type of reallocation at given position. More...
 
bool isReopenStandardStream (const Token *tok) const
 Check if token reopens a standard stream. More...
 
bool isOpenDevNull (const Token *tok) const
 Check if token opens /dev/null. More...
 
void memleakError (const Token *tok, const std::string &varname) const
 Report that there is a memory leak (new/malloc/etc) More...
 
void resourceLeakError (const Token *tok, const std::string &varname) const
 Report that there is a resource leak (fopen/popen/etc) More...
 
void deallocuseError (const Token *tok, const std::string &varname) const
 
void mismatchAllocDealloc (const std::list< const Token * > &callstack, const std::string &varname) const
 
void memleakUponReallocFailureError (const Token *tok, const std::string &reallocfunction, const std::string &varname) const
 
AllocType functionReturnType (const Function *func, std::list< const Function * > *callstack=nullptr) const
 What type of allocated memory does the given function return? More...
 

Private Member Functions

void reportErr (const Token *tok, Severity severity, const std::string &id, const std::string &msg, const CWE &cwe) const
 Report error. More...
 
void reportErr (const std::list< const Token * > &callstack, Severity severity, const std::string &id, const std::string &msg, const CWE &cwe) const
 Report error. More...
 

Private Attributes

const Tokenizer *const mTokenizer_
 For access to the tokens. More...
 
ErrorLogger *const mErrorLogger_
 ErrorLogger used to report errors. More...
 
const Settings *const mSettings_
 Enabled standards. More...
 

Detailed Description

Base class for memory leaks checking.

Definition at line 55 of file checkmemoryleak.h.

Member Enumeration Documentation

◆ AllocType

What type of allocation are used.

. the "Many" means that several types of allocation and deallocation are used

Enumerator
No 
Malloc 
New 
NewArray 
File 
Fd 
Pipe 
OtherMem 
OtherRes 
Many 

Definition at line 95 of file checkmemoryleak.h.

Constructor & Destructor Documentation

◆ CheckMemoryLeak() [1/3]

CheckMemoryLeak::CheckMemoryLeak ( )
delete

◆ CheckMemoryLeak() [2/3]

CheckMemoryLeak::CheckMemoryLeak ( const CheckMemoryLeak )
delete

◆ CheckMemoryLeak() [3/3]

CheckMemoryLeak::CheckMemoryLeak ( const Tokenizer t,
ErrorLogger e,
const Settings s 
)
inline

Definition at line 91 of file checkmemoryleak.h.

Member Function Documentation

◆ deallocuseError()

void CheckMemoryLeak::deallocuseError ( const Token tok,
const std::string &  varname 
) const

◆ functionReturnType()

CheckMemoryLeak::AllocType CheckMemoryLeak::functionReturnType ( const Function func,
std::list< const Function * > *  callstack = nullptr 
) const

◆ getAllocationType()

CheckMemoryLeak::AllocType CheckMemoryLeak::getAllocationType ( const Token tok2,
nonneg int  varid,
std::list< const Function * > *  callstack = nullptr 
) const

◆ getDeallocationType()

CheckMemoryLeak::AllocType CheckMemoryLeak::getDeallocationType ( const Token tok,
nonneg int  varid 
) const

◆ getReallocationType()

CheckMemoryLeak::AllocType CheckMemoryLeak::getReallocationType ( const Token tok2,
nonneg int  varid 
) const

◆ isOpenDevNull()

bool CheckMemoryLeak::isOpenDevNull ( const Token tok) const

Check if token opens /dev/null.

Parameters
toktoken to check

Definition at line 256 of file checkmemoryleak.cpp.

References getArguments(), Settings::hasLib(), mSettings_, numberOfArguments(), Token::simpleMatch(), and Token::str().

Referenced by CheckMemoryLeakNoVar::checkForUnusedReturnValue().

◆ isReopenStandardStream()

bool CheckMemoryLeak::isReopenStandardStream ( const Token tok) const

◆ memleakError()

void CheckMemoryLeak::memleakError ( const Token tok,
const std::string &  varname 
) const

Report that there is a memory leak (new/malloc/etc)

Parameters
toktoken where memory is leaked
varnamename of variable

Definition at line 302 of file checkmemoryleak.cpp.

References CWE, error, and reportErr().

Referenced by CheckMemoryLeakInFunction::getErrorMessages(), CheckLeakAutoVar::leakError(), and memoryLeak().

◆ memleakUponReallocFailureError()

void CheckMemoryLeak::memleakUponReallocFailureError ( const Token tok,
const std::string &  reallocfunction,
const std::string &  varname 
) const

◆ memoryLeak()

void CheckMemoryLeak::memoryLeak ( const Token tok,
const std::string &  varname,
AllocType  alloctype 
) const

◆ mismatchAllocDealloc()

void CheckMemoryLeak::mismatchAllocDealloc ( const std::list< const Token * > &  callstack,
const std::string &  varname 
) const

◆ operator=()

CheckMemoryLeak& CheckMemoryLeak::operator= ( const CheckMemoryLeak )
delete

◆ reportErr() [1/2]

void CheckMemoryLeak::reportErr ( const std::list< const Token * > &  callstack,
Severity  severity,
const std::string &  id,
const std::string &  msg,
const CWE cwe 
) const
private

Report error.

Similar with the function Check::reportError

Parameters
callstackcallstack of error
severitythe severity of the bug
idtype of message
msgtext
cwecwe number

Definition at line 293 of file checkmemoryleak.cpp.

References Tokenizer::list, mErrorLogger_, mTokenizer_, normal, ErrorLogger::reportErr(), and Check::writeToErrorList().

◆ reportErr() [2/2]

void CheckMemoryLeak::reportErr ( const Token tok,
Severity  severity,
const std::string &  id,
const std::string &  msg,
const CWE cwe 
) const
private

Report error.

Similar with the function Check::reportError

Parameters
tokthe token where the error occurs
severitythe severity of the bug
idtype of message
msgtext
cwecwe number

Definition at line 283 of file checkmemoryleak.cpp.

Referenced by deallocuseError(), memleakError(), memleakUponReallocFailureError(), mismatchAllocDealloc(), and resourceLeakError().

◆ resourceLeakError()

void CheckMemoryLeak::resourceLeakError ( const Token tok,
const std::string &  varname 
) const

Report that there is a resource leak (fopen/popen/etc)

Parameters
toktoken where resource is leaked
varnamename of variable

Definition at line 312 of file checkmemoryleak.cpp.

References CWE, error, and reportErr().

Referenced by CheckMemoryLeakInFunction::getErrorMessages(), CheckLeakAutoVar::leakError(), and memoryLeak().

Member Data Documentation

◆ mErrorLogger_

ErrorLogger* const CheckMemoryLeak::mErrorLogger_
private

ErrorLogger used to report errors.

Definition at line 61 of file checkmemoryleak.h.

Referenced by reportErr().

◆ mSettings_

const Settings* const CheckMemoryLeak::mSettings_
private

◆ mTokenizer_

const Tokenizer* const CheckMemoryLeak::mTokenizer_
private

For access to the tokens.

Definition at line 58 of file checkmemoryleak.h.

Referenced by reportErr().


The documentation for this class was generated from the following files: