Cppcheck
|
Checking for uninitialized variables. More...
#include <checkuninitvar.h>
Public Types | |
enum | Alloc { NO_ALLOC , NO_CTOR_CALL , CTOR_CALL , ARRAY } |
Public Member Functions | |
CheckUninitVar () | |
This constructor is used when registering the CheckUninitVar. More... | |
const Token * | isVariableUsage (const Token *vartok, bool pointer, Alloc alloc, int indirect=0) const |
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 const Token * | isVariableUsage (const Token *vartok, const Library &library, bool pointer, Alloc alloc, int indirect=0) |
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 | |
CheckUninitVar (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... | |
bool | diag (const Token *tok) |
void | check () |
Check for uninitialized variables. More... | |
void | checkScope (const Scope *scope, const std::set< std::string > &arrayTypeDefs) |
void | checkStruct (const Token *tok, const Variable &structvar) |
bool | checkScopeForVariable (const Token *tok, const Variable &var, bool *const possibleInit, bool *const noreturn, Alloc *const alloc, const std::string &membervar, std::map< nonneg int, VariableValue > &variableValue) |
const Token * | checkExpr (const Token *tok, const Variable &var, const Alloc alloc, bool known, bool *bailout=nullptr) const |
bool | checkIfForWhileHead (const Token *startparentheses, const Variable &var, bool suppressErrors, bool isuninit, Alloc alloc, const std::string &membervar) |
bool | checkLoopBody (const Token *tok, const Variable &var, const Alloc alloc, const std::string &membervar, const bool suppressErrors) |
const Token * | checkLoopBodyRecursive (const Token *start, const Variable &var, const Alloc alloc, const std::string &membervar, bool &bailout) const |
recursively check loop, return error token More... | |
void | checkRhs (const Token *tok, const Variable &var, Alloc alloc, nonneg int number_of_if, const std::string &membervar) |
int | isFunctionParUsage (const Token *vartok, bool pointer, Alloc alloc, int indirect=0) const |
bool | isMemberVariableAssignment (const Token *tok, const std::string &membervar) const |
bool | isMemberVariableUsage (const Token *tok, bool isPointer, Alloc alloc, const std::string &membervar) const |
void | valueFlowUninit () |
ValueFlow-based checking for uninitialized variables. More... | |
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 | uninitvarError (const Token *tok, const ValueFlow::Value &v) |
void | uninitdataError (const Token *tok, const std::string &varname) |
void | uninitvarError (const Token *tok, const std::string &varname, ErrorPath errorPath) |
void | uninitvarError (const Token *tok, const std::string &varname) |
void | uninitvarError (const Token *tok, const std::string &varname, Alloc alloc) |
void | uninitStructMemberError (const Token *tok, const std::string &membername) |
void | getErrorMessages (ErrorLogger *errorLogger, const Settings *settings) const override |
get error messages More... | |
std::string | classInfo () const override |
get information about this class, used to generate documentation More... | |
Static Private Member Functions | |
static int | isFunctionParUsage (const Token *vartok, const Library &library, bool pointer, Alloc alloc, int indirect=0) |
static std::string | myName () |
Private Attributes | |
std::set< const Token * > | mUninitDiags |
Friends | |
class | TestUninitVar |
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 {} |
Checking for uninitialized variables.
Definition at line 65 of file checkuninitvar.h.
Enumerator | |
---|---|
NO_ALLOC | |
NO_CTOR_CALL | |
CTOR_CALL | |
ARRAY |
Definition at line 72 of file checkuninitvar.h.
|
inline |
This constructor is used when registering the CheckUninitVar.
Definition at line 70 of file checkuninitvar.h.
|
inlineprivate |
This constructor is used when running checks.
Definition at line 79 of file checkuninitvar.h.
|
overrideprivatevirtual |
Analyse all file infos for all TU.
Reimplemented from Check.
Definition at line 1723 of file checkuninitvar.cpp.
References CTU::FileInfo::FunctionCall::callArgumentExpression, CWE_USE_OF_UNINITIALIZED_VARIABLE, emptyString, error, CTU::FileInfo::getCallsMap(), CTU::FileInfo::getErrorPath(), CTU::FileInfo::UnsafeUsage::myArgumentName, normal, ErrorLogger::reportErr(), and CTU::FileInfo::uninit.
|
private |
Check for uninitialized variables.
Definition at line 115 of file checkuninitvar.cpp.
References checkScope(), Tokenizer::getSymbolDatabase(), Scope::isExecutable(), Check::logChecker(), Token::Match(), Check::mTokenizer, Token::next(), SymbolDatabase::scopeList, and Tokenizer::tokens().
Referenced by runChecks().
|
private |
Definition at line 853 of file checkuninitvar.cpp.
References Token::astOperand1(), Token::astOperand2(), bailout, Variable::declarationId(), Variable::isPointer(), isUnevaluated(), isVariableUsage(), Token::Match(), Token::previous(), and Token::varId().
Referenced by checkScopeForVariable().
|
private |
Definition at line 882 of file checkuninitvar.cpp.
References Variable::declarationId(), Token::getValue(), isMemberVariableAssignment(), isMemberVariableUsage(), Variable::isPointer(), isUnevaluated(), isVariableUsage(), Token::link(), Token::Match(), Token::next(), Token::tokAt(), uninitStructMemberError(), and uninitvarError().
Referenced by checkScopeForVariable().
|
private |
Definition at line 1068 of file checkuninitvar.cpp.
References bailout, checkLoopBodyRecursive(), Token::expressionString(), uninitStructMemberError(), and uninitvarError().
Referenced by checkScope(), and checkScopeForVariable().
|
private |
recursively check loop, return error token
Definition at line 919 of file checkuninitvar.cpp.
References astIsRHS(), Token::astParent(), bailout, Variable::declarationId(), isMemberVariableAssignment(), isMemberVariableUsage(), Variable::isPointer(), isUnevaluated(), isVariableUsage(), Token::link(), Token::linkAt(), Token::Match(), Token::next(), none, op1_and_op2, Token::previous(), Token::simpleMatch(), Token::str(), Token::tokAt(), and visitAstNodes().
Referenced by checkLoopBody().
|
private |
Definition at line 1084 of file checkuninitvar.cpp.
References Token::astOperand2(), Variable::declarationId(), indent(), isMemberVariableUsage(), Variable::isPointer(), isUnevaluated(), isVariableUsage(), isVariableUsed(), Token::linkAt(), Token::Match(), Variable::nameToken(), Token::next(), Token::previous(), Token::str(), uninitStructMemberError(), uninitvarError(), and Token::varId().
Referenced by checkScope(), and checkScopeForVariable().
|
private |
Definition at line 135 of file checkuninitvar.cpp.
References Function::argumentList, ARRAY, Token::astParent(), Scope::bodyEnd, Scope::bodyStart, checkLoopBody(), checkRhs(), checkScopeForVariable(), checkStruct(), ValueType::containerTypeToken, Variable::declarationId(), emptyString, findAllocFuncCallToken(), Scope::function, Library::getAllocFuncInfo(), getVariableValues(), Library::AllocFunc::initData, Variable::isArray(), Token::isC(), Tokenizer::isCPP(), Token::isEnumType(), Variable::isExtern(), Variable::isPointer(), Variable::isPointerToArray(), Variable::isReference(), Token::isStandardType(), Variable::isStatic(), Variable::isStlType(), Type::isStructType(), Variable::isThrow(), Settings::library, Token::link(), Token::linkAt(), Token::Match(), Check::mSettings, Check::mTokenizer, Variable::nameToken(), Type::needInitialization, Token::next(), NO_ALLOC, Token::simpleMatch(), Token::str(), Token::strAt(), Type::True, Variable::type(), Variable::typeStartToken(), Variable::valueType(), and Scope::varlist.
Referenced by check().
|
private |
Definition at line 401 of file checkuninitvar.cpp.
References astIsLHS(), astIsVariableComparison(), Token::astOperand1(), Token::astOperand2(), Token::astParent(), checkExpr(), checkIfForWhileHead(), checkLoopBody(), checkRhs(), conditionAlwaysTrueOrFalse(), CTOR_CALL, debug, Settings::debugwarnings, Variable::declarationId(), emptyString, Token::findmatch(), Library::getAllocFuncInfo(), Token::getKnownIntValue(), Token::hasKnownIntValue(), Library::AllocFunc::initData, Variable::isArray(), Token::isCast(), Token::isCpp(), Tokenizer::isCPP(), Token::isEnumType(), isMemberVariableAssignment(), isMemberVariableUsage(), Variable::isPointer(), Tokenizer::isScopeNoReturn(), Token::isStandardType(), isUnevaluated(), isVariableUsage(), isVariableUsed(), Settings::library, Token::link(), Token::linkAt(), Token::Match(), Check::mSettings, Check::mTokenizer, Variable::name(), Variable::nameToken(), Type::needInitialization, Token::next(), NO_CTOR_CALL, none, nonneg, Scope::numConstructors, Token::previous(), Check::reportError(), Token::simpleMatch(), Token::str(), Token::strAt(), Token::tokAt(), Type::True, Variable::type(), Variable::typeScope(), Variable::typeStartToken(), uninitStructMemberError(), uninitvarError(), Token::values(), Token::variable(), Token::varId(), and visitAstNodes().
Referenced by checkScope(), and checkStruct().
Definition at line 240 of file checkuninitvar.cpp.
References Scope::bodyEnd, Scope::bodyStart, checkScopeForVariable(), SymbolDatabase::classAndStructScopes, Scope::className, Scope::eUnion, Tokenizer::getSymbolDatabase(), getVariableValues(), Variable::hasDefault(), Variable::isArray(), Tokenizer::isC(), Variable::isClass(), Variable::isStatic(), Token::linenr(), Token::Match(), Check::mTokenizer, Variable::name(), Type::needInitialization, Scope::nestedList, Token::next(), NO_ALLOC, Scope::numConstructors, Token::str(), Token::tokAt(), Type::True, Variable::type(), Scope::type, Variable::typeStartToken(), and Scope::varlist.
Referenced by checkScope().
|
inlineoverrideprivatevirtual |
get information about this class, used to generate documentation
Implements Check.
Definition at line 147 of file checkuninitvar.h.
|
private |
Definition at line 106 of file checkuninitvar.cpp.
References Token::astParent(), Token::Match(), and mUninitDiags.
Referenced by uninitvarError().
|
inlineoverrideprivatevirtual |
get error messages
Implements Check.
Definition at line 132 of file checkuninitvar.h.
References uninitdataError(), uninitStructMemberError(), and uninitvarError().
|
overrideprivatevirtual |
Parse current TU and extract file info.
Reimplemented from Check.
Definition at line 1701 of file checkuninitvar.cpp.
References CTU::getUnsafeUsage(), and isVariableUsage().
|
private |
Definition at line 1419 of file checkuninitvar.cpp.
References isFunctionParUsage(), Settings::library, and Check::mSettings.
|
staticprivate |
Definition at line 1351 of file checkuninitvar.cpp.
References Token::function(), Function::getArgumentVar(), getAstParentSkipPossibleCastAndAddressOf(), Token::isName(), Library::isnullargbad(), Library::isuninitargbad(), Token::link(), Token::linkAt(), Token::Match(), NO_ALLOC, Token::previous(), Token::simpleMatch(), Token::str(), Variable::typeStartToken(), and Token::variable().
Referenced by isFunctionParUsage(), isMemberVariableAssignment(), isMemberVariableUsage(), and isVariableUsage().
|
private |
Definition at line 1424 of file checkuninitvar.cpp.
References Library::ArgumentChecks::DIR_IN, Library::ArgumentChecks::DIR_OUT, Token::function(), Library::getArgDirection(), Token::isConstOp(), isFunctionParUsage(), isLikelyStreamRead(), Token::isName(), Settings::library, Token::link(), Token::Match(), Check::mSettings, NO_ALLOC, Token::previous(), Token::simpleMatch(), Token::str(), Token::strAt(), Token::tokAt(), and Variable::typeStartToken().
Referenced by checkIfForWhileHead(), checkLoopBodyRecursive(), checkScopeForVariable(), and isMemberVariableUsage().
|
private |
Definition at line 1493 of file checkuninitvar.cpp.
References Token::astOperand1(), Token::astParent(), isFunctionParUsage(), isMemberVariableAssignment(), Token::isUnaryOp(), isVariableUsage(), Token::Match(), Token::next(), Token::previous(), Token::simpleMatch(), Token::strAt(), Token::tokAt(), and Token::variable().
Referenced by checkIfForWhileHead(), checkLoopBodyRecursive(), checkRhs(), and checkScopeForVariable().
const Token * CheckUninitVar::isVariableUsage | ( | const Token * | vartok, |
bool | pointer, | ||
Alloc | alloc, | ||
int | indirect = 0 |
||
) | const |
Definition at line 1340 of file checkuninitvar.cpp.
References isVariableUsage(), Settings::library, and Check::mSettings.
|
static |
Definition at line 1136 of file checkuninitvar.cpp.
References ARRAY, astIsLhs(), astIsRhs(), Token::astOperand1(), Token::astParent(), CTOR_CALL, Token::isBinaryOp(), Token::isCast(), Token::isCpp(), isFunctionParUsage(), isLikelyStreamRead(), Token::isName(), Variable::isReference(), Token::isUnaryOp(), isVoidCast(), Token::Match(), Variable::nameToken(), NO_ALLOC, ValueType::pointer, Token::previous(), Token::simpleMatch(), Token::str(), ValueType::type, Token::valueType(), and Token::variable().
Referenced by checkExpr(), checkIfForWhileHead(), checkLoopBodyRecursive(), checkRhs(), checkScopeForVariable(), getFileInfo(), isMemberVariableUsage(), isVariableUsage(), isVariableUsage(), and valueFlowSubFunction().
|
overrideprivatevirtual |
Reimplemented from Check.
Definition at line 1712 of file checkuninitvar.cpp.
References CTU::loadUnsafeUsageListFromXml().
|
inlinestaticprivate |
Definition at line 143 of file checkuninitvar.h.
|
inlineoverrideprivatevirtual |
Run checks against the normal token list.
Implements Check.
Definition at line 83 of file checkuninitvar.h.
References check(), Tokenizer::getSettings(), and valueFlowUninit().
|
private |
Definition at line 1535 of file checkuninitvar.cpp.
References CWE_USE_OF_UNINITIALIZED_VARIABLE, error, normal, and Check::reportError().
Referenced by getErrorMessages().
|
private |
Definition at line 1590 of file checkuninitvar.cpp.
References CWE_USE_OF_UNINITIALIZED_VARIABLE, error, normal, and Check::reportError().
Referenced by checkIfForWhileHead(), checkLoopBody(), checkRhs(), checkScopeForVariable(), and getErrorMessages().
|
inlineprivate |
Definition at line 119 of file checkuninitvar.h.
|
inlineprivate |
Definition at line 122 of file checkuninitvar.h.
|
private |
Definition at line 1540 of file checkuninitvar.cpp.
References CWE_USE_OF_UNINITIALIZED_VARIABLE, diag(), error, normal, and Check::reportError().
|
private |
Definition at line 1553 of file checkuninitvar.cpp.
References astIsRHS(), Token::astParent(), CWE_USE_OF_UNINITIALIZED_VARIABLE, diag(), error, ValueFlow::Value::errorPath, Token::expressionString(), inconclusive, Settings::isEnabled(), ValueFlow::Value::isInconclusive(), ValueFlow::Value::isKnown(), Check::mSettings, normal, Check::reportError(), Token::simpleMatch(), ValueFlow::Value::subexpressions, and warning.
Referenced by checkIfForWhileHead(), checkLoopBody(), checkRhs(), checkScopeForVariable(), getErrorMessages(), and valueFlowUninit().
|
private |
ValueFlow-based checking for uninitialized variables.
Definition at line 1598 of file checkuninitvar.cpp.
References Token::astParent(), Scope::bodyEnd, Scope::bodyStart, SymbolDatabase::functionScopes, getExprUsage(), Tokenizer::getSymbolDatabase(), Inconclusive, inconclusive, isLeafDot(), CheckNullPointer::isPointerDeRef(), Token::isUnaryOp(), isUnevaluated(), ValueFlow::Value::isUninitValue(), isVariableChanged(), isVariableChangedByFunctionCall(), isVoidCast(), Check::logChecker(), Token::Match(), Check::mSettings, Check::mTokenizer, Token::next(), NotUsed, PassedByReference, Token::simpleMatch(), uninitvarError(), and Used.
Referenced by runChecks().
|
friend |
Definition at line 66 of file checkuninitvar.h.
|
private |
Definition at line 130 of file checkuninitvar.h.
Referenced by diag().