Cppcheck
Public Member Functions | Static Public Member Functions | Private Member Functions | Static Private Member Functions | Friends | List of all members
CheckNullPointer Class Reference

check for null pointer dereferencing More...

#include <checknullpointer.h>

Inheritance diagram for CheckNullPointer:
Check

Public Member Functions

 CheckNullPointer ()
 This constructor is used when registering the CheckNullPointer. More...
 
bool isPointerDeRef (const Token *tok, bool &unknown) const
 Is there a pointer dereference? Everything that should result in a nullpointer dereference error message will result in a true return value. More...
 
- Public Member Functions inherited from Check
 Check (const std::string &aname)
 This constructor is used when registering the CheckClass. More...
 
virtual ~Check ()
 
 Check (const Check &)=delete
 
Checkoperator= (const Check &)=delete
 
const std::string & name () const
 class name, used to generate documentation More...
 

Static Public Member Functions

static bool isPointerDeRef (const Token *tok, bool &unknown, const Settings &settings)
 
- Static Public Member Functions inherited from Check
static std::list< Check * > & instances ()
 List of registered check classes. More...
 
static void writeToErrorList (const ErrorMessage &errmsg)
 Write given error to stdout in xml format. More...
 

Private Member Functions

 CheckNullPointer (const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
 This constructor is used when running checks. More...
 
void runChecks (const Tokenizer &tokenizer, ErrorLogger *errorLogger) override
 Run checks against the normal token list. More...
 
void nullPointer ()
 possible null pointer dereference More...
 
void nullConstantDereference ()
 dereferencing null constant (after Tokenizer::simplifyKnownVariables) More...
 
void nullPointerError (const Token *tok)
 
void nullPointerError (const Token *tok, const std::string &varname, const ValueFlow::Value *value, bool inconclusive)
 
Check::FileInfogetFileInfo (const Tokenizer &tokenizer, const Settings &settings) const override
 Parse current TU and extract file info. More...
 
Check::FileInfoloadFileInfoFromXml (const tinyxml2::XMLElement *xmlElement) const override
 
bool analyseWholeProgram (const CTU::FileInfo *ctu, const std::list< Check::FileInfo * > &fileInfo, const Settings &settings, ErrorLogger &errorLogger) override
 Analyse all file infos for all TU. More...
 
void getErrorMessages (ErrorLogger *errorLogger, const Settings *settings) const override
 Get error messages. More...
 
std::string classInfo () const override
 class info in WIKI format. More...
 
void nullPointerByDeRefAndChec ()
 Does one part of the check for nullPointer(). More...
 
void arithmetic ()
 undefined null pointer arithmetic More...
 
void pointerArithmeticError (const Token *tok, const ValueFlow::Value *value, bool inconclusive)
 
void redundantConditionWarning (const Token *tok, const ValueFlow::Value *value, const Token *condition, bool inconclusive)
 

Static Private Member Functions

static void parseFunctionCall (const Token &tok, std::list< const Token * > &var, const Library &library)
 parse a function call and extract information about variable usage More...
 
static std::string myName ()
 Name of check. More...
 

Friends

class TestNullPointer
 

Additional Inherited Members

- Protected Member Functions inherited from Check
 Check (std::string aname, const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
 This constructor is used when running checks. More...
 
void reportError (const Token *tok, const Severity severity, const std::string &id, const std::string &msg)
 report an error More...
 
void reportError (const Token *tok, const Severity severity, const std::string &id, const std::string &msg, const CWE &cwe, Certainty certainty)
 report an error More...
 
void reportError (const std::list< const Token * > &callstack, Severity severity, const std::string &id, const std::string &msg)
 report an error More...
 
void reportError (const std::list< const Token * > &callstack, Severity severity, const std::string &id, const std::string &msg, const CWE &cwe, Certainty certainty)
 report an error More...
 
void reportError (const ErrorPath &errorPath, Severity severity, const char id[], const std::string &msg, const CWE &cwe, Certainty certainty)
 
void logChecker (const char id[])
 log checker More...
 
ErrorPath getErrorPath (const Token *errtok, const ValueFlow::Value *value, std::string bug) const
 
bool wrongData (const Token *tok, const char *str)
 Use WRONG_DATA in checkers when you check for wrong data. More...
 
- Static Protected Member Functions inherited from Check
static std::string getMessageId (const ValueFlow::Value &value, const char id[])
 
- Protected Attributes inherited from Check
const Tokenizer *const mTokenizer {}
 
const Settings *const mSettings {}
 
ErrorLogger *const mErrorLogger {}
 

Detailed Description

check for null pointer dereferencing

Definition at line 52 of file checknullpointer.h.

Constructor & Destructor Documentation

◆ CheckNullPointer() [1/2]

CheckNullPointer::CheckNullPointer ( )
inline

This constructor is used when registering the CheckNullPointer.

Definition at line 57 of file checknullpointer.h.

◆ CheckNullPointer() [2/2]

CheckNullPointer::CheckNullPointer ( const Tokenizer tokenizer,
const Settings settings,
ErrorLogger errorLogger 
)
inlineprivate

This constructor is used when running checks.

Definition at line 84 of file checknullpointer.h.

Member Function Documentation

◆ analyseWholeProgram()

bool CheckNullPointer::analyseWholeProgram ( const CTU::FileInfo ctu,
const std::list< Check::FileInfo * > &  fileInfo,
const Settings settings,
ErrorLogger errorLogger 
)
overrideprivatevirtual

◆ arithmetic()

void CheckNullPointer::arithmetic ( )
private

◆ classInfo()

std::string CheckNullPointer::classInfo ( ) const
inlineoverrideprivatevirtual

class info in WIKI format.

Used by –doc

Implements Check.

Definition at line 130 of file checknullpointer.h.

◆ getErrorMessages()

void CheckNullPointer::getErrorMessages ( ErrorLogger errorLogger,
const Settings settings 
) const
inlineoverrideprivatevirtual

Get error messages.

Used by –errorlist

Implements Check.

Definition at line 117 of file checknullpointer.h.

References nullPointerError(), pointerArithmeticError(), and redundantConditionWarning().

◆ getFileInfo()

Check::FileInfo * CheckNullPointer::getFileInfo ( const Tokenizer tokenizer,
const Settings settings 
) const
overrideprivatevirtual

Parse current TU and extract file info.

Reimplemented from Check.

Definition at line 581 of file checknullpointer.cpp.

References CTU::getUnsafeUsage(), and isUnsafeUsage().

◆ isPointerDeRef() [1/2]

bool CheckNullPointer::isPointerDeRef ( const Token tok,
bool &  unknown 
) const

Is there a pointer dereference? Everything that should result in a nullpointer dereference error message will result in a true return value.

If it's unknown if the pointer is dereferenced false is returned.

Parameters
toktoken for the pointer
unknownit is not known if there is a pointer dereference (could be reported as a debug message)
Returns
true => there is a dereference

Definition at line 147 of file checknullpointer.cpp.

References Check::mSettings.

Referenced by CheckStl::checkDereferenceInvalidIterator2(), CheckLeakAutoVar::checkTokenInsideExpression(), isPointerDeRef(), isUnsafeUsage(), nullPointerByDeRefAndChec(), and CheckUninitVar::valueFlowUninit().

◆ isPointerDeRef() [2/2]

bool CheckNullPointer::isPointerDeRef ( const Token tok,
bool &  unknown,
const Settings settings 
)
static

◆ loadFileInfoFromXml()

Check::FileInfo * CheckNullPointer::loadFileInfoFromXml ( const tinyxml2::XMLElement *  xmlElement) const
overrideprivatevirtual

Reimplemented from Check.

Definition at line 592 of file checknullpointer.cpp.

References CTU::loadUnsafeUsageListFromXml().

◆ myName()

static std::string CheckNullPointer::myName ( )
inlinestaticprivate

Name of check.

Definition at line 125 of file checknullpointer.h.

◆ nullConstantDereference()

void CheckNullPointer::nullConstantDereference ( )
private

◆ nullPointer()

void CheckNullPointer::nullPointer ( )
private

possible null pointer dereference

Definition at line 319 of file checknullpointer.cpp.

References Check::logChecker(), and nullPointerByDeRefAndChec().

Referenced by runChecks().

◆ nullPointerByDeRefAndChec()

void CheckNullPointer::nullPointerByDeRefAndChec ( )
private

◆ nullPointerError() [1/2]

void CheckNullPointer::nullPointerError ( const Token tok)
inlineprivate

◆ nullPointerError() [2/2]

void CheckNullPointer::nullPointerError ( const Token tok,
const std::string &  varname,
const ValueFlow::Value value,
bool  inconclusive 
)
private

◆ parseFunctionCall()

void CheckNullPointer::parseFunctionCall ( const Token tok,
std::list< const Token * > &  var,
const Library library 
)
staticprivate

parse a function call and extract information about variable usage

Parameters
tokfirst token
varvariables that the function read / write.
library–library files data

Definition at line 65 of file checknullpointer.cpp.

References checkNullpointerFunctionCallPlausibility(), Token::eString, Library::formatstr_argno(), Library::formatstr_function(), Library::formatstr_scan(), Token::function(), getArguments(), Variable::isArrayOrPointer(), Library::isnullargbad(), Variable::isStlStringType(), Token::Match(), and Token::tokAt().

Referenced by isPointerDeRef(), and nullConstantDereference().

◆ pointerArithmeticError()

void CheckNullPointer::pointerArithmeticError ( const Token tok,
const ValueFlow::Value value,
bool  inconclusive 
)
private

◆ redundantConditionWarning()

void CheckNullPointer::redundantConditionWarning ( const Token tok,
const ValueFlow::Value value,
const Token condition,
bool  inconclusive 
)
private

◆ runChecks()

void CheckNullPointer::runChecks ( const Tokenizer tokenizer,
ErrorLogger errorLogger 
)
inlineoverrideprivatevirtual

Run checks against the normal token list.

Implements Check.

Definition at line 88 of file checknullpointer.h.

References arithmetic(), Tokenizer::getSettings(), nullConstantDereference(), and nullPointer().

Friends And Related Function Documentation

◆ TestNullPointer

friend class TestNullPointer
friend

Definition at line 53 of file checknullpointer.h.


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