MCPcopy Create free account
hub / github.com/cppcheck-opensource/cppcheck / negativeIndex

Method negativeIndex

lib/checkstl.cpp:1361–1383  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1359}
1360
1361void CheckStlImpl::negativeIndex()
1362{
1363 logChecker("CheckStl::negativeIndex");
1364
1365 // Negative index is out of bounds..
1366 const SymbolDatabase* const symbolDatabase = mTokenizer->getSymbolDatabase();
1367 for (const Scope * scope : symbolDatabase->functionScopes) {
1368 for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
1369 if (!Token::Match(tok, "%var% [") || !tok->next()->astOperand2())
1370 continue;
1371 const Variable * const var = tok->variable();
1372 if (!var || tok == var->nameToken())
1373 continue;
1374 const Library::Container * const container = mSettings.library.detectContainer(var->typeStartToken());
1375 if (!container || !container->arrayLike_indexOp)
1376 continue;
1377 const ValueFlow::Value *index = tok->next()->astOperand2()->getValueLE(-1, mSettings);
1378 if (!index)
1379 continue;
1380 negativeIndexError(tok, *index);
1381 }
1382 }
1383}
1384
1385void CheckStlImpl::negativeIndexError(const Token *tok, const ValueFlow::Value &index)
1386{

Callers 1

runChecksMethod · 0.45

Calls 6

nextMethod · 0.80
astOperand2Method · 0.80
variableMethod · 0.80
detectContainerMethod · 0.80
getValueLEMethod · 0.80
nameTokenMethod · 0.45

Tested by

no test coverage detected