53 :
Check(myName(), tokenizer, settings, errorLogger) {}
57 if (!tokenizer.
isCPP()) {
96 void outOfBoundsIndexExpression();
102 void stlOutOfBounds();
107 void negativeIndex();
115 void invalidContainer();
117 bool checkIteratorPair(
const Token* tok1,
const Token* tok2);
123 void mismatchingContainers();
125 void mismatchingContainerIterator();
132 void eraseCheckLoopVar(
const Scope& scope,
const Variable* var);
137 void stlBoundaries();
142 void checkFindInsert();
153 void redundantCondition();
161 void missingComparison();
170 void checkDereferenceInvalidIterator();
171 void checkDereferenceInvalidIterator2();
180 void dereferenceErasedError(
const Token* erased,
const Token* deref,
const std::string& itername,
bool inconclusive);
183 void useStlAlgorithm();
185 void knownEmptyContainer();
187 void eraseIteratorOutOfBounds();
191 bool isContainerSize(
const Token *containerToken,
const Token *expr)
const;
192 bool isContainerSizeGE(
const Token * containerToken,
const Token *expr)
const;
194 void missingComparisonError(
const Token* incrementToken1,
const Token* incrementToken2);
195 void string_c_strThrowError(
const Token* tok);
196 void string_c_strError(
const Token* tok);
197 void string_c_strReturn(
const Token* tok);
198 void string_c_strParam(
const Token* tok,
nonneg int number,
const std::string& argtype =
"std::string");
199 void string_c_strConstructor(
const Token* tok,
const std::string& argtype =
"std::string");
200 void string_c_strAssignment(
const Token* tok,
const std::string& argtype =
"std::string");
201 void string_c_strConcat(
const Token* tok);
202 void string_c_strStream(
const Token* tok);
205 void outOfBoundsIndexExpressionError(
const Token *tok,
const Token *index);
206 void stlOutOfBoundsError(
const Token* tok,
const std::string& num,
const std::string& var,
bool at);
208 void invalidIteratorError(
const Token* tok,
const std::string& iteratorName);
209 void iteratorsError(
const Token* tok,
const std::string& containerName1,
const std::string& containerName2);
210 void iteratorsError(
const Token* tok,
const Token* containerTok,
const std::string& containerName1,
const std::string& containerName2);
211 void iteratorsError(
const Token* tok,
const Token* containerTok,
const std::string& containerName);
212 void mismatchingContainerIteratorError(
const Token* containerTok,
const Token* iterTok,
const Token* containerTok2);
213 void mismatchingContainersError(
const Token* tok1,
const Token* tok2);
214 void mismatchingContainerExpressionError(
const Token *tok1,
const Token *tok2);
215 void sameIteratorExpressionError(
const Token *tok);
216 void stlBoundariesError(
const Token* tok);
217 void if_findError(
const Token* tok,
bool str);
218 void checkFindInsertError(
const Token *tok);
219 void sizeError(
const Token* tok);
220 void redundantIfRemoveError(
const Token* tok);
221 void invalidContainerLoopError(
const Token* tok,
const Token* loopTok,
ErrorPath errorPath);
223 void invalidContainerReferenceError(
const Token* tok,
const Token* contTok,
ErrorPath errorPath);
225 void uselessCallsReturnValueError(
const Token* tok,
const std::string& varname,
const std::string&
function);
226 void uselessCallsSwapError(
const Token* tok,
const std::string& varname);
228 void uselessCallsSubstrError(
const Token* tok, SubstrErrorType type);
229 void uselessCallsEmptyError(
const Token* tok);
230 void uselessCallsRemoveError(
const Token* tok,
const std::string&
function);
231 void uselessCallsConstructorError(
const Token* tok);
233 void dereferenceInvalidIteratorError(
const Token* deref,
const std::string& iterName);
236 void useStlAlgorithmError(
const Token *tok,
const std::string &algoName);
238 void knownEmptyContainerError(
const Token *tok,
const std::string& algo);
242 void globalLockGuardError(
const Token *tok);
243 void localMutexError(
const Token *tok);
246 CheckStl c(
nullptr, settings, errorLogger);
290 return "Check for invalid usage of STL:\n"
291 "- out of bounds errors\n"
292 "- misuse of iterators when iterating through a container\n"
293 "- mismatching containers in calls\n"
294 "- same iterators in calls\n"
295 "- dereferencing an erased iterator\n"
296 "- for vectors: using iterator/pointer after push_back has been used\n"
297 "- optimisation: use empty() instead of size() to guarantee fast code\n"
298 "- suspicious condition when using find\n"
299 "- unnecessary searching in associative containers\n"
300 "- redundant condition\n"
301 "- common mistakes when using string::c_str()\n"
302 "- useless calls of string and STL functions\n"
303 "- dereferencing an invalid iterator\n"
304 "- erasing an iterator that is out of bounds\n"
305 "- reading from empty STL container\n"
306 "- iterating over an empty STL container\n"
307 "- consider using an STL algorithm instead of raw loop\n"
308 "- incorrect locking with mutex\n";
Check STL usage (invalidation of iterators, mismatching containers, etc)
CheckStl()
This constructor is used when registering the CheckClass.
void outOfBoundsIndexExpression()
Accessing container out of bounds, following index expression.
void missingComparison()
Missing inner comparison, when incrementing iterator inside loop Dangers:
void erase()
Dangerous usage of erase.
void missingComparisonError(const Token *incrementToken1, const Token *incrementToken2)
void sizeError(const Token *tok)
void uselessCalls()
Check calls that using them is useless
void checkDereferenceInvalidIterator2()
void stlBoundariesError(const Token *tok)
void uselessCallsSubstrError(const Token *tok, SubstrErrorType type)
void invalidContainerError(const Token *tok, const Token *contTok, const ValueFlow::Value *val, ErrorPath errorPath)
void mismatchingContainerIteratorError(const Token *containerTok, const Token *iterTok, const Token *containerTok2)
void uselessCallsSwapError(const Token *tok, const std::string &varname)
std::string classInfo() const override
get information about this class, used to generate documentation
void knownEmptyContainerError(const Token *tok, const std::string &algo)
void size()
Suggest using empty() instead of checking size() against zero for containers.
void mismatchingContainersError(const Token *tok1, const Token *tok2)
void eraseIteratorOutOfBoundsError(const Token *ftok, const Token *itertok, const ValueFlow::Value *val=nullptr)
void uselessCallsRemoveError(const Token *tok, const std::string &function)
void uselessCallsReturnValueError(const Token *tok, const std::string &varname, const std::string &function)
void sameIteratorExpressionError(const Token *tok)
void stlOutOfBounds()
Finds errors like this: for (unsigned ii = 0; ii <= foo.size(); ++ii)
void string_c_strParam(const Token *tok, nonneg int number, const std::string &argtype="std::string")
void dereferenceInvalidIteratorError(const Token *deref, const std::string &iterName)
void invalidIteratorError(const Token *tok, const std::string &iteratorName)
void negativeIndexError(const Token *tok, const ValueFlow::Value &index)
void outOfBounds()
Accessing container out of bounds using ValueFlow.
void mismatchingContainerIterator()
CheckStl(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
This constructor is used when running checks.
void negativeIndex()
negative index for array like containers
void dereferenceErasedError(const Token *erased, const Token *deref, const std::string &itername, bool inconclusive)
Dereferencing an erased iterator.
void mismatchingContainerExpressionError(const Token *tok1, const Token *tok2)
void if_find()
if (a.find(x)) - possibly incorrect condition
void stlOutOfBoundsError(const Token *tok, const std::string &num, const std::string &var, bool at)
void checkFindInsertError(const Token *tok)
void useStlAlgorithm()
Look for loops that can replaced with std algorithms.
void iterators()
Finds errors like this: for (it = foo.begin(); it != bar.end(); ++it)
void string_c_strThrowError(const Token *tok)
void useStlAlgorithmError(const Token *tok, const std::string &algoName)
void outOfBoundsError(const Token *tok, const std::string &containerName, const ValueFlow::Value *containerSize, const std::string &index, const ValueFlow::Value *indexValue)
void stlBoundaries()
bad condition.
void string_c_str()
Check for common mistakes when using the function string::c_str()
void mismatchingContainers()
Mismatching containers: std::find(foo.begin(), bar.end(), x)
void knownEmptyContainer()
void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override
run checks, the token list is not simplified
void globalLockGuardError(const Token *tok)
void checkDereferenceInvalidIterator()
Check for dereferencing an iterator that is invalid
void iteratorsError(const Token *tok, const std::string &containerName1, const std::string &containerName2)
void redundantCondition()
Check for redundant condition 'if (ints.find(1) != ints.end()) ints.remove(123);'.
void localMutexError(const Token *tok)
void redundantIfRemoveError(const Token *tok)
void eraseIteratorOutOfBounds()
void string_c_strError(const Token *tok)
void if_findError(const Token *tok, bool str)
void string_c_strReturn(const Token *tok)
void invalidContainerLoopError(const Token *tok, const Token *loopTok, ErrorPath errorPath)
static std::string myName()
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override
get error messages
void uselessCallsEmptyError(const Token *tok)
Interface class that cppcheck uses to communicate with the checks.
This is an interface, which the class responsible of error logging should implement.
This is just a container for general settings so that we don't need to pass individual values to func...
The token list that the TokenList generates is a linked-list of this class.
The main purpose is to tokenize the source code.
const Settings & getSettings() const
bool isCPP() const
Is the code CPP.
Information about a member variable.
static const std::string emptyString
std::list< ErrorPathItem > ErrorPath