Cppcheck
Public Member Functions | Private Types | Private Member Functions | Static Private Member Functions | List of all members
CheckStl Class Reference

Check STL usage (invalidation of iterators, mismatching containers, etc) More...

#include <checkstl.h>

Inheritance diagram for CheckStl:
Check

Public Member Functions

 CheckStl ()
 This constructor is used when registering the CheckClass. 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...
 
virtual FileInfogetFileInfo (const Tokenizer &, const Settings &) const
 
virtual FileInfoloadFileInfoFromXml (const tinyxml2::XMLElement *xmlElement) const
 
virtual bool analyseWholeProgram (const CTU::FileInfo *ctu, const std::list< FileInfo * > &fileInfo, const Settings &, ErrorLogger &)
 

Private Types

enum class  SubstrErrorType { EMPTY , COPY , PREFIX , PREFIX_CONCAT }
 

Private Member Functions

 CheckStl (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, the token list is not simplified More...
 
void outOfBounds ()
 Accessing container out of bounds using ValueFlow. More...
 
void outOfBoundsIndexExpression ()
 Accessing container out of bounds, following index expression. More...
 
void stlOutOfBounds ()
 Finds errors like this: for (unsigned ii = 0; ii <= foo.size(); ++ii) More...
 
void negativeIndex ()
 negative index for array like containers More...
 
void iterators ()
 Finds errors like this: for (it = foo.begin(); it != bar.end(); ++it) More...
 
void invalidContainer ()
 
bool checkIteratorPair (const Token *tok1, const Token *tok2)
 
void mismatchingContainers ()
 Mismatching containers: std::find(foo.begin(), bar.end(), x) More...
 
void mismatchingContainerIterator ()
 
void erase ()
 Dangerous usage of erase. More...
 
void eraseCheckLoopVar (const Scope &scope, const Variable *var)
 
void stlBoundaries ()
 bad condition. More...
 
void if_find ()
 if (a.find(x)) - possibly incorrect condition More...
 
void checkFindInsert ()
 
void size ()
 Suggest using empty() instead of checking size() against zero for containers. More...
 
void redundantCondition ()
 Check for redundant condition 'if (ints.find(1) != ints.end()) ints.remove(123);'. More...
 
void missingComparison ()
 Missing inner comparison, when incrementing iterator inside loop Dangers: More...
 
void string_c_str ()
 Check for common mistakes when using the function string::c_str() More...
 
void uselessCalls ()
 Check calls that using them is useless More...
 
void checkDereferenceInvalidIterator ()
 Check for dereferencing an iterator that is invalid More...
 
void checkDereferenceInvalidIterator2 ()
 
void dereferenceErasedError (const Token *erased, const Token *deref, const std::string &itername, bool inconclusive)
 Dereferencing an erased iterator. More...
 
void useStlAlgorithm ()
 Look for loops that can replaced with std algorithms. More...
 
void knownEmptyContainer ()
 
void eraseIteratorOutOfBounds ()
 
void checkMutexes ()
 
bool isContainerSize (const Token *containerToken, const Token *expr) const
 
bool isContainerSizeGE (const Token *containerToken, const Token *expr) const
 
void missingComparisonError (const Token *incrementToken1, const Token *incrementToken2)
 
void string_c_strThrowError (const Token *tok)
 
void string_c_strError (const Token *tok)
 
void string_c_strReturn (const Token *tok)
 
void string_c_strParam (const Token *tok, nonneg int number, const std::string &argtype="std::string")
 
void string_c_strConstructor (const Token *tok, const std::string &argtype="std::string")
 
void string_c_strAssignment (const Token *tok, const std::string &argtype="std::string")
 
void string_c_strConcat (const Token *tok)
 
void string_c_strStream (const Token *tok)
 
void outOfBoundsError (const Token *tok, const std::string &containerName, const ValueFlow::Value *containerSize, const std::string &index, const ValueFlow::Value *indexValue)
 
void outOfBoundsIndexExpressionError (const Token *tok, const Token *index)
 
void stlOutOfBoundsError (const Token *tok, const std::string &num, const std::string &var, bool at)
 
void negativeIndexError (const Token *tok, const ValueFlow::Value &index)
 
void invalidIteratorError (const Token *tok, const std::string &iteratorName)
 
void iteratorsError (const Token *tok, const std::string &containerName1, const std::string &containerName2)
 
void iteratorsError (const Token *tok, const Token *containerTok, const std::string &containerName1, const std::string &containerName2)
 
void iteratorsError (const Token *tok, const Token *containerTok, const std::string &containerName)
 
void mismatchingContainerIteratorError (const Token *containerTok, const Token *iterTok, const Token *containerTok2)
 
void mismatchingContainersError (const Token *tok1, const Token *tok2)
 
void mismatchingContainerExpressionError (const Token *tok1, const Token *tok2)
 
void sameIteratorExpressionError (const Token *tok)
 
void stlBoundariesError (const Token *tok)
 
void if_findError (const Token *tok, bool str)
 
void checkFindInsertError (const Token *tok)
 
void sizeError (const Token *tok)
 
void redundantIfRemoveError (const Token *tok)
 
void invalidContainerLoopError (const Token *tok, const Token *loopTok, ErrorPath errorPath)
 
void invalidContainerError (const Token *tok, const Token *contTok, const ValueFlow::Value *val, ErrorPath errorPath)
 
void invalidContainerReferenceError (const Token *tok, const Token *contTok, ErrorPath errorPath)
 
void uselessCallsReturnValueError (const Token *tok, const std::string &varname, const std::string &function)
 
void uselessCallsSwapError (const Token *tok, const std::string &varname)
 
void uselessCallsSubstrError (const Token *tok, SubstrErrorType type)
 
void uselessCallsEmptyError (const Token *tok)
 
void uselessCallsRemoveError (const Token *tok, const std::string &function)
 
void uselessCallsConstructorError (const Token *tok)
 
void dereferenceInvalidIteratorError (const Token *deref, const std::string &iterName)
 
void dereferenceInvalidIteratorError (const Token *tok, const ValueFlow::Value *value, bool inconclusive)
 
void useStlAlgorithmError (const Token *tok, const std::string &algoName)
 
void knownEmptyContainerError (const Token *tok, const std::string &algo)
 
void eraseIteratorOutOfBoundsError (const Token *ftok, const Token *itertok, const ValueFlow::Value *val=nullptr)
 
void globalLockGuardError (const Token *tok)
 
void localMutexError (const Token *tok)
 
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 std::string myName ()
 

Additional Inherited Members

- 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...
 
- 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 STL usage (invalidation of iterators, mismatching containers, etc)

Definition at line 45 of file checkstl.h.

Member Enumeration Documentation

◆ SubstrErrorType

enum CheckStl::SubstrErrorType
strongprivate
Enumerator
EMPTY 
COPY 
PREFIX 
PREFIX_CONCAT 

Definition at line 227 of file checkstl.h.

Constructor & Destructor Documentation

◆ CheckStl() [1/2]

CheckStl::CheckStl ( )
inline

This constructor is used when registering the CheckClass.

Definition at line 48 of file checkstl.h.

◆ CheckStl() [2/2]

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

This constructor is used when running checks.

Definition at line 52 of file checkstl.h.

Member Function Documentation

◆ checkDereferenceInvalidIterator()

void CheckStl::checkDereferenceInvalidIterator ( )
private

◆ checkDereferenceInvalidIterator2()

void CheckStl::checkDereferenceInvalidIterator2 ( )
private

◆ checkFindInsert()

void CheckStl::checkFindInsert ( )
private

◆ checkFindInsertError()

void CheckStl::checkFindInsertError ( const Token tok)
private

◆ checkIteratorPair()

bool CheckStl::checkIteratorPair ( const Token tok1,
const Token tok2 
)
private

◆ checkMutexes()

void CheckStl::checkMutexes ( )
private

◆ classInfo()

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

get information about this class, used to generate documentation

Implements Check.

Definition at line 289 of file checkstl.h.

◆ dereferenceErasedError()

void CheckStl::dereferenceErasedError ( const Token erased,
const Token deref,
const std::string &  itername,
bool  inconclusive 
)
private

Dereferencing an erased iterator.

Parameters
erasedtoken where the erase occurs
dereftoken where the dereference occurs
iternameiterator name
inconclusiveinconclusive flag

Definition at line 394 of file checkstl.cpp.

References CWE664, error, inconclusive, normal, and Check::reportError().

Referenced by eraseCheckLoopVar(), getErrorMessages(), and iterators().

◆ dereferenceInvalidIteratorError() [1/2]

void CheckStl::dereferenceInvalidIteratorError ( const Token deref,
const std::string &  iterName 
)
private

◆ dereferenceInvalidIteratorError() [2/2]

void CheckStl::dereferenceInvalidIteratorError ( const Token tok,
const ValueFlow::Value value,
bool  inconclusive 
)
private

◆ erase()

void CheckStl::erase ( )
private

Dangerous usage of erase.

The iterator is invalidated by erase so it is bad to dereference it after the erase.

Definition at line 1379 of file checkstl.cpp.

References Scope::classDef, Scope::eFor, eraseCheckLoopVar(), Scope::eWhile, Tokenizer::getSymbolDatabase(), Token::isName(), Token::linkAt(), Check::logChecker(), Token::Match(), Check::mTokenizer, Token::previous(), SymbolDatabase::scopeList, Token::simpleMatch(), Token::tokAt(), Scope::type, and Token::variable().

Referenced by runChecks().

◆ eraseCheckLoopVar()

void CheckStl::eraseCheckLoopVar ( const Scope scope,
const Variable var 
)
private

◆ eraseIteratorOutOfBounds()

void CheckStl::eraseIteratorOutOfBounds ( )
private

◆ eraseIteratorOutOfBoundsError()

void CheckStl::eraseIteratorOutOfBoundsError ( const Token ftok,
const Token itertok,
const ValueFlow::Value val = nullptr 
)
private

◆ getErrorMessages()

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

◆ globalLockGuardError()

void CheckStl::globalLockGuardError ( const Token tok)
private

Definition at line 3240 of file checkstl.cpp.

References CWE833, normal, Check::reportError(), and warning.

Referenced by checkMutexes(), and getErrorMessages().

◆ if_find()

void CheckStl::if_find ( )
private

◆ if_findError()

void CheckStl::if_findError ( const Token tok,
bool  str 
)
private

◆ invalidContainer()

void CheckStl::invalidContainer ( )
private

◆ invalidContainerError()

void CheckStl::invalidContainerError ( const Token tok,
const Token contTok,
const ValueFlow::Value val,
ErrorPath  errorPath 
)
private

◆ invalidContainerLoopError()

void CheckStl::invalidContainerLoopError ( const Token tok,
const Token loopTok,
ErrorPath  errorPath 
)
private

Definition at line 1214 of file checkstl.cpp.

References CWE664, error, normal, Check::reportError(), and Token::str().

Referenced by getErrorMessages(), and invalidContainer().

◆ invalidContainerReferenceError()

void CheckStl::invalidContainerReferenceError ( const Token tok,
const Token contTok,
ErrorPath  errorPath 
)
private

Definition at line 1239 of file checkstl.cpp.

References CWE664, error, Token::expressionString(), Check::name(), normal, and Check::reportError().

Referenced by invalidContainer().

◆ invalidIteratorError()

void CheckStl::invalidIteratorError ( const Token tok,
const std::string &  iteratorName 
)
private

Definition at line 358 of file checkstl.cpp.

References CWE664, error, normal, and Check::reportError().

Referenced by getErrorMessages(), and iterators().

◆ isContainerSize()

bool CheckStl::isContainerSize ( const Token containerToken,
const Token expr 
) const
private

◆ isContainerSizeGE()

bool CheckStl::isContainerSizeGE ( const Token containerToken,
const Token expr 
) const
private

◆ iterators()

void CheckStl::iterators ( )
private

◆ iteratorsError() [1/3]

void CheckStl::iteratorsError ( const Token tok,
const std::string &  containerName1,
const std::string &  containerName2 
)
private

Definition at line 363 of file checkstl.cpp.

References CWE664, error, normal, and Check::reportError().

Referenced by checkIteratorPair(), getErrorMessages(), and iterators().

◆ iteratorsError() [2/3]

void CheckStl::iteratorsError ( const Token tok,
const Token containerTok,
const std::string &  containerName 
)
private

Definition at line 380 of file checkstl.cpp.

References CWE664, error, normal, and Check::reportError().

◆ iteratorsError() [3/3]

void CheckStl::iteratorsError ( const Token tok,
const Token containerTok,
const std::string &  containerName1,
const std::string &  containerName2 
)
private

Definition at line 371 of file checkstl.cpp.

References CWE664, error, normal, and Check::reportError().

◆ knownEmptyContainer()

void CheckStl::knownEmptyContainer ( )
private

◆ knownEmptyContainerError()

void CheckStl::knownEmptyContainerError ( const Token tok,
const std::string &  algo 
)
private

◆ localMutexError()

void CheckStl::localMutexError ( const Token tok)
private

Definition at line 3247 of file checkstl.cpp.

References CWE667, normal, Check::reportError(), and warning.

Referenced by checkMutexes(), and getErrorMessages().

◆ mismatchingContainerExpressionError()

void CheckStl::mismatchingContainerExpressionError ( const Token tok1,
const Token tok2 
)
private

Definition at line 675 of file checkstl.cpp.

References CWE664, Token::expressionString(), normal, Check::reportError(), and warning.

Referenced by checkIteratorPair(), and getErrorMessages().

◆ mismatchingContainerIterator()

void CheckStl::mismatchingContainerIterator ( )
private

◆ mismatchingContainerIteratorError()

void CheckStl::mismatchingContainerIteratorError ( const Token containerTok,
const Token iterTok,
const Token containerTok2 
)
private

◆ mismatchingContainers()

void CheckStl::mismatchingContainers ( )
private

◆ mismatchingContainersError()

void CheckStl::mismatchingContainersError ( const Token tok1,
const Token tok2 
)
private

◆ missingComparison()

void CheckStl::missingComparison ( )
private

Missing inner comparison, when incrementing iterator inside loop Dangers:

  • may increment iterator beyond end
  • may unintentionally skip elements in list/set etc

Definition at line 1860 of file checkstl.cpp.

References Token::astParent(), Scope::bodyEnd, Scope::bodyStart, Scope::classDef, Scope::eFor, Tokenizer::getSymbolDatabase(), SimpleEnableGroup< T >::isEnabled(), Token::linkAt(), Check::logChecker(), Token::Match(), missingComparisonError(), Check::mSettings, Check::mTokenizer, Token::next(), SymbolDatabase::scopeList, Settings::severity, Token::simpleMatch(), Token::tokAt(), Scope::type, and warning.

Referenced by runChecks().

◆ missingComparisonError()

void CheckStl::missingComparisonError ( const Token incrementToken1,
const Token incrementToken2 
)
private

Definition at line 1924 of file checkstl.cpp.

References CWE834, Token::linenr(), normal, Check::reportError(), and warning.

Referenced by getErrorMessages(), and missingComparison().

◆ myName()

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

Definition at line 285 of file checkstl.h.

◆ negativeIndex()

void CheckStl::negativeIndex ( )
private

◆ negativeIndexError()

void CheckStl::negativeIndexError ( const Token tok,
const ValueFlow::Value index 
)
private

◆ outOfBounds()

void CheckStl::outOfBounds ( )
private

◆ outOfBoundsError()

void CheckStl::outOfBoundsError ( const Token tok,
const std::string &  containerName,
const ValueFlow::Value containerSize,
const std::string &  index,
const ValueFlow::Value indexValue 
)
private

◆ outOfBoundsIndexExpression()

void CheckStl::outOfBoundsIndexExpression ( )
private

◆ outOfBoundsIndexExpressionError()

void CheckStl::outOfBoundsIndexExpressionError ( const Token tok,
const Token index 
)
private

◆ redundantCondition()

void CheckStl::redundantCondition ( )
private

◆ redundantIfRemoveError()

void CheckStl::redundantIfRemoveError ( const Token tok)
private

Definition at line 1852 of file checkstl.cpp.

References CWE398, normal, Check::reportError(), and style.

Referenced by getErrorMessages(), and redundantCondition().

◆ runChecks()

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

◆ sameIteratorExpressionError()

void CheckStl::sameIteratorExpressionError ( const Token tok)
private

Definition at line 684 of file checkstl.cpp.

References CWE664, normal, Check::reportError(), and style.

Referenced by getErrorMessages(), and mismatchingContainers().

◆ size()

void CheckStl::size ( )
private

◆ sizeError()

void CheckStl::sizeError ( const Token tok)
private

Definition at line 1807 of file checkstl.cpp.

References CWE398, normal, performance, Check::reportError(), and Token::str().

Referenced by getErrorMessages(), and size().

◆ stlBoundaries()

void CheckStl::stlBoundaries ( )
private

◆ stlBoundariesError()

void CheckStl::stlBoundariesError ( const Token tok)
private

Definition at line 1469 of file checkstl.cpp.

References CWE664, error, normal, and Check::reportError().

Referenced by getErrorMessages(), and stlBoundaries().

◆ stlOutOfBounds()

void CheckStl::stlOutOfBounds ( )
private

◆ stlOutOfBoundsError()

void CheckStl::stlOutOfBoundsError ( const Token tok,
const std::string &  num,
const std::string &  var,
bool  at 
)
private

Definition at line 1333 of file checkstl.cpp.

References CWE788, error, normal, and Check::reportError().

Referenced by getErrorMessages(), and stlOutOfBounds().

◆ string_c_str()

void CheckStl::string_c_str ( )
private

◆ string_c_strAssignment()

void CheckStl::string_c_strAssignment ( const Token tok,
const std::string &  argtype = "std::string" 
)
private

Definition at line 2191 of file checkstl.cpp.

References CWE704, normal, performance, and Check::reportError().

Referenced by string_c_str().

◆ string_c_strConcat()

void CheckStl::string_c_strConcat ( const Token tok)
private

Definition at line 2198 of file checkstl.cpp.

References CWE704, normal, performance, and Check::reportError().

Referenced by string_c_str().

◆ string_c_strConstructor()

void CheckStl::string_c_strConstructor ( const Token tok,
const std::string &  argtype = "std::string" 
)
private

Definition at line 2184 of file checkstl.cpp.

References CWE704, normal, performance, and Check::reportError().

Referenced by string_c_str().

◆ string_c_strError()

void CheckStl::string_c_strError ( const Token tok)
private

Definition at line 2164 of file checkstl.cpp.

References CWE664, error, normal, and Check::reportError().

Referenced by getErrorMessages(), and string_c_str().

◆ string_c_strParam()

void CheckStl::string_c_strParam ( const Token tok,
nonneg int  number,
const std::string &  argtype = "std::string" 
)
private

Definition at line 2176 of file checkstl.cpp.

References CWE704, normal, performance, and Check::reportError().

Referenced by getErrorMessages(), and string_c_str().

◆ string_c_strReturn()

void CheckStl::string_c_strReturn ( const Token tok)
private

Definition at line 2170 of file checkstl.cpp.

References CWE704, normal, performance, and Check::reportError().

Referenced by getErrorMessages(), and string_c_str().

◆ string_c_strStream()

void CheckStl::string_c_strStream ( const Token tok)
private

Definition at line 2205 of file checkstl.cpp.

References CWE704, normal, performance, and Check::reportError().

Referenced by string_c_str().

◆ string_c_strThrowError()

void CheckStl::string_c_strThrowError ( const Token tok)
private

Definition at line 2158 of file checkstl.cpp.

References error, and Check::reportError().

Referenced by getErrorMessages(), and string_c_str().

◆ uselessCalls()

void CheckStl::uselessCalls ( )
private

◆ uselessCallsConstructorError()

void CheckStl::uselessCallsConstructorError ( const Token tok)
private

Definition at line 2330 of file checkstl.cpp.

References CWE398, normal, performance, Check::reportError(), and Token::str().

Referenced by uselessCalls().

◆ uselessCallsEmptyError()

void CheckStl::uselessCallsEmptyError ( const Token tok)
private

Definition at line 2336 of file checkstl.cpp.

References CWE398, normal, Check::reportError(), and warning.

Referenced by getErrorMessages(), and uselessCalls().

◆ uselessCallsRemoveError()

void CheckStl::uselessCallsRemoveError ( const Token tok,
const std::string &  function 
)
private

Definition at line 2341 of file checkstl.cpp.

References CWE762, normal, Check::reportError(), and warning.

Referenced by getErrorMessages(), and uselessCalls().

◆ uselessCallsReturnValueError()

void CheckStl::uselessCallsReturnValueError ( const Token tok,
const std::string &  varname,
const std::string &  function 
)
private

Definition at line 2287 of file checkstl.cpp.

References CWE628, normal, Check::reportError(), and warning.

Referenced by getErrorMessages(), and uselessCalls().

◆ uselessCallsSubstrError()

void CheckStl::uselessCallsSubstrError ( const Token tok,
SubstrErrorType  type 
)
private

Definition at line 2310 of file checkstl.cpp.

References COPY, CWE398, EMPTY, normal, performance, PREFIX, PREFIX_CONCAT, and Check::reportError().

Referenced by getErrorMessages(), and uselessCalls().

◆ uselessCallsSwapError()

void CheckStl::uselessCallsSwapError ( const Token tok,
const std::string &  varname 
)
private

Definition at line 2300 of file checkstl.cpp.

References CWE628, normal, performance, and Check::reportError().

Referenced by getErrorMessages(), and uselessCalls().

◆ useStlAlgorithm()

void CheckStl::useStlAlgorithm ( )
private

◆ useStlAlgorithmError()

void CheckStl::useStlAlgorithmError ( const Token tok,
const std::string &  algoName 
)
private

Definition at line 2550 of file checkstl.cpp.

References CWE398, normal, Check::reportError(), and style.

Referenced by getErrorMessages(), and useStlAlgorithm().


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