Cppcheck
|
check for null pointer dereferencing More...
#include <checknullpointer.h>
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 | |
Check & | operator= (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::FileInfo * | getFileInfo (const Tokenizer &tokenizer, const Settings &settings) const override |
Parse current TU and extract file info. More... | |
Check::FileInfo * | loadFileInfoFromXml (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 {} |
check for null pointer dereferencing
Definition at line 52 of file checknullpointer.h.
|
inline |
This constructor is used when registering the CheckNullPointer.
Definition at line 57 of file checknullpointer.h.
|
inlineprivate |
This constructor is used when running checks.
Definition at line 84 of file checknullpointer.h.
|
overrideprivatevirtual |
Analyse all file infos for all TU.
Reimplemented from Check.
Definition at line 603 of file checknullpointer.cpp.
References CWE_NULL_POINTER_DEREFERENCE, emptyString, error, CTU::FileInfo::getCallsMap(), CTU::FileInfo::getErrorPath(), SimpleEnableGroup< T >::isEnabled(), Check::logChecker(), CTU::FileInfo::UnsafeUsage::myArgumentName, normal, CTU::FileInfo::null, ErrorLogger::reportErr(), Settings::severity, and warning.
|
private |
undefined null pointer arithmetic
Definition at line 466 of file checknullpointer.cpp.
References Token::astOperand1(), Token::astOperand2(), Scope::bodyEnd, Scope::bodyStart, Settings::certainty, ValueFlow::Value::condition, SymbolDatabase::functionScopes, Tokenizer::getSymbolDatabase(), Token::getValue(), inconclusive, ValueFlow::Value::intvalue, SimpleEnableGroup< T >::isEnabled(), ValueFlow::Value::isInconclusive(), ValueType::isIntegral(), Check::logChecker(), Token::Match(), Check::mSettings, Check::mTokenizer, Token::next(), pointerArithmeticError(), redundantConditionWarning(), Settings::severity, Token::valueType(), and warning.
Referenced by pointerArithmeticError(), redundantConditionWarning(), and runChecks().
|
inlineoverrideprivatevirtual |
class info in WIKI format.
Used by –doc
Implements Check.
Definition at line 130 of file checknullpointer.h.
|
inlineoverrideprivatevirtual |
Get error messages.
Used by –errorlist
Implements Check.
Definition at line 117 of file checknullpointer.h.
References nullPointerError(), pointerArithmeticError(), and redundantConditionWarning().
|
overrideprivatevirtual |
Parse current TU and extract file info.
Reimplemented from Check.
Definition at line 581 of file checknullpointer.cpp.
References CTU::getUnsafeUsage(), and isUnsafeUsage().
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.
tok | token for the pointer |
unknown | it is not known if there is a pointer dereference (could be reported as a debug message) |
Definition at line 147 of file checknullpointer.cpp.
References Check::mSettings.
Referenced by CheckStl::checkDereferenceInvalidIterator2(), CheckLeakAutoVar::checkTokenInsideExpression(), isPointerDeRef(), isUnsafeUsage(), nullPointerByDeRefAndChec(), and CheckUninitVar::valueFlowUninit().
|
static |
Definition at line 152 of file checknullpointer.cpp.
References astIsRHS(), Token::astOperand1(), Token::astOperand2(), Token::astParent(), astParentSkipParens(), Variable::isArray(), Variable::isPointer(), isPointerDeRef(), Variable::isStlStringType(), Variable::isStlType(), Token::isUnaryOp(), isUnevaluated(), Settings::library, Token::link(), Token::Match(), Token::originalName(), parseFunctionCall(), Token::previous(), Token::scope(), Token::str(), Token::tokAt(), Variable::typeStartToken(), Token::variable(), and Token::varId().
|
overrideprivatevirtual |
Reimplemented from Check.
Definition at line 592 of file checknullpointer.cpp.
References CTU::loadUnsafeUsageListFromXml().
|
inlinestaticprivate |
Name of check.
Definition at line 125 of file checknullpointer.h.
|
private |
dereferencing null constant (after Tokenizer::simplifyKnownVariables)
Dereferencing null constant (simplified token list)
Definition at line 333 of file checknullpointer.cpp.
References Scope::bodyEnd, Scope::bodyStart, Scope::function, SymbolDatabase::functionScopes, getArguments(), Tokenizer::getSymbolDatabase(), Function::hasBody(), Token::hasKnownIntValue(), Variable::isArray(), Function::isConstructor(), Library::isnullargbad(), Token::isOp(), Variable::isPointer(), Variable::isStlStringType(), Variable::isStlType(), isUnevaluated(), Settings::library, Token::link(), Check::logChecker(), Token::Match(), Check::mSettings, Check::mTokenizer, Token::next(), nullPointerError(), Token::originalName(), parseFunctionCall(), Token::previous(), Token::simpleMatch(), Token::str(), Token::strAt(), Token::tokAt(), Function::token, Token::values(), Token::variable(), and Token::varId().
Referenced by runChecks().
|
private |
possible null pointer dereference
Definition at line 319 of file checknullpointer.cpp.
References Check::logChecker(), and nullPointerByDeRefAndChec().
Referenced by runChecks().
|
private |
Does one part of the check for nullPointer().
Dereferencing a pointer and then checking if it's NULL..
Definition at line 282 of file checknullpointer.cpp.
References Settings::certainty, inconclusive, SimpleEnableGroup< T >::isEnabled(), ValueFlow::Value::isInconclusive(), isNullablePointer(), isPointerDeRef(), isUnevaluated(), Token::Match(), Check::mSettings, Check::mTokenizer, Token::next(), nullPointerError(), and Tokenizer::tokens().
Referenced by nullPointer().
|
inlineprivate |
Definition at line 101 of file checknullpointer.h.
References emptyString, and ValueFlow::Value::setKnown().
Referenced by getErrorMessages(), nullConstantDereference(), and nullPointerByDeRefAndChec().
|
private |
Definition at line 427 of file checknullpointer.cpp.
References ValueFlow::Value::condition, CWE_NULL_POINTER_DEREFERENCE, ValueFlow::Value::defaultArg, ValueFlow::eitherTheConditionIsRedundant(), error, Check::getErrorPath(), inconclusive, Settings::isEnabled(), ValueFlow::Value::isInconclusive(), ValueFlow::Value::isKnown(), Settings::isPremiumEnabled(), Check::mSettings, normal, Check::reportError(), and warning.
|
staticprivate |
parse a function call and extract information about variable usage
tok | first token |
var | variables 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().
|
private |
Definition at line 513 of file checknullpointer.cpp.
References arithmetic(), arithmeticTypeString(), CWE_INCORRECT_CALCULATION, error, Check::getErrorPath(), inconclusive, normal, Check::reportError(), and Token::str().
Referenced by arithmetic(), and getErrorMessages().
|
private |
Definition at line 532 of file checknullpointer.cpp.
References arithmetic(), arithmeticTypeString(), CWE_INCORRECT_CALCULATION, ValueFlow::eitherTheConditionIsRedundant(), Check::getErrorPath(), inconclusive, normal, Check::reportError(), Token::str(), and warning.
Referenced by arithmetic(), and getErrorMessages().
|
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().
|
friend |
Definition at line 53 of file checknullpointer.h.