Change every 'tokAt(1)->' to 'next()->' because 'tokAt(1) == 0' is equivalent to 'next() == 0', hence the equivalent cppcheck crash.

This commit is contained in:
Edoardo Prezioso 2011-11-20 15:09:57 +01:00
parent 42e369a4b4
commit 11dd3c09ce
8 changed files with 29 additions and 29 deletions

View File

@ -52,7 +52,7 @@ void Check64BitPortability::pointerassignment()
if (Token::Match(tok, "[;{}] %var% = %var% [;+]")) {
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
const Variable *var1(symbolDatabase->getVariableFromVarId(tok->tokAt(1)->varId()));
const Variable *var1(symbolDatabase->getVariableFromVarId(tok->next()->varId()));
const Variable *var2(symbolDatabase->getVariableFromVarId(tok->tokAt(3)->varId()));
if (isaddr(var1) && isint(var2) && tok->strAt(4) != "+")

View File

@ -105,7 +105,7 @@ void CheckAutoVariables::autoVariables()
} else if (Token::Match(tok, "[;{}] %var% . %var% = & %var%")) {
// TODO: check if the parameter is only changed temporarily (#2969)
if (_settings->inconclusive) {
const Variable * var1 = symbolDatabase->getVariableFromVarId(tok->tokAt(1)->varId());
const Variable * var1 = symbolDatabase->getVariableFromVarId(tok->next()->varId());
if (var1 && var1->isArgument() && Token::Match(var1->nameToken()->tokAt(-2), "%type% *")) {
const Variable * var2 = symbolDatabase->getVariableFromVarId(tok->tokAt(6)->varId());
if (var2 && var2->isLocal() && !var2->isStatic() && !Token::simpleMatch(var2->typeEndToken(), "*"))
@ -116,7 +116,7 @@ void CheckAutoVariables::autoVariables()
} else if (Token::Match(tok, "[;{}] %var% . %var% = %var% ;")) {
// TODO: check if the parameter is only changed temporarily (#2969)
if (_settings->inconclusive) {
const Variable * var1 = symbolDatabase->getVariableFromVarId(tok->tokAt(1)->varId());
const Variable * var1 = symbolDatabase->getVariableFromVarId(tok->next()->varId());
if (var1 && var1->isArgument() && Token::Match(var1->nameToken()->tokAt(-2), "%type% *")) {
const Variable * var2 = symbolDatabase->getVariableFromVarId(tok->tokAt(5)->varId());
if (var2 && var2->isLocal() && var2->isArray() && !var2->isStatic())

View File

@ -1301,12 +1301,12 @@ void CheckBufferOverrun::checkGlobalAndLocalVariable()
if (indentlevel > 0 && Token::Match(tok, "[*;{}] %var% = new %type% [ %num% ]")) {
size = MathLib::toLongNumber(tok->strAt(6));
type = tok->strAt(4);
varid = tok->tokAt(1)->varId();
varid = tok->next()->varId();
nextTok = 8;
} else if (indentlevel > 0 && Token::Match(tok, "[*;{}] %var% = new %type% ( %num% )")) {
size = 1;
type = tok->strAt(4);
varid = tok->tokAt(1)->varId();
varid = tok->next()->varId();
nextTok = 8;
} else if (indentlevel > 0 &&
Token::Match(tok, "[;{}] %var% = %str% ;") &&
@ -1319,7 +1319,7 @@ void CheckBufferOverrun::checkGlobalAndLocalVariable()
} else if (indentlevel > 0 && Token::Match(tok, "[*;{}] %var% = malloc|alloca ( %num% ) ;")) {
size = MathLib::toLongNumber(tok->strAt(5));
type = "char"; // minimum type, typesize=1
varid = tok->tokAt(1)->varId();
varid = tok->next()->varId();
nextTok = 7;
if (varid > 0) {

View File

@ -985,7 +985,7 @@ bool CheckClass::hasAssignSelf(const Function *func, const Token *rhs)
for (const Token *tok = func->start; tok && tok != last; tok = tok->next()) {
if (Token::simpleMatch(tok, "if (")) {
const Token *tok1 = tok->tokAt(2);
const Token *tok2 = tok->tokAt(1)->link();
const Token *tok2 = tok->next()->link();
if (tok1 && tok2) {
for (; tok1 && tok1 != tok2; tok1 = tok1->next()) {

View File

@ -2275,18 +2275,18 @@ void CheckMemoryLeakInFunction::checkReallocUsage()
}
memleakUponReallocFailureError(tok, tok->str());
} else if (tok->tokAt(1)->varId() > 0 &&
} else if (tok->next()->varId() > 0 &&
(Token::Match(tok, "* %var% = realloc|g_try_realloc ( * %var% , %any%") &&
tok->tokAt(1)->varId() == tok->tokAt(6)->varId())&&
parameterVarIds.find(tok->tokAt(1)->varId()) == parameterVarIds.end()) {
tok->next()->varId() == tok->tokAt(6)->varId())&&
parameterVarIds.find(tok->next()->varId()) == parameterVarIds.end()) {
// Check that another copy of the pointer wasn't saved earlier in the function
if (Token::findmatch(startOfFunction, "%var% = * %varid% ;", tok->tokAt(1)->varId()) ||
Token::findmatch(startOfFunction, "[{};] * %varid% = %var% [;=]", tok->tokAt(1)->varId()))
if (Token::findmatch(startOfFunction, "%var% = * %varid% ;", tok->next()->varId()) ||
Token::findmatch(startOfFunction, "[{};] * %varid% = %var% [;=]", tok->next()->varId()))
continue;
const Token* tokEndRealloc = tok->linkAt(4);
// Check that the allocation isn't followed immediately by an 'if (!var) { error(); }' that might handle failure
if (Token::Match(tokEndRealloc->tokAt(1), "; if ( ! * %varid% ) {", tok->tokAt(1)->varId())) {
if (Token::Match(tokEndRealloc->tokAt(1), "; if ( ! * %varid% ) {", tok->next()->varId())) {
const Token* tokEndBrace = tokEndRealloc->linkAt(8);
if (tokEndBrace && Token::simpleMatch(tokEndBrace->tokAt(-2), ") ;") &&
Token::Match(tokEndBrace->linkAt(-2)->tokAt(-2), "{|}|; %var% ("))

View File

@ -587,9 +587,9 @@ void CheckOther::checkSwitchCaseFallThrough()
std::stack<Token *> scopenest;
bool justbreak = true;
bool firstcase = true;
for (const Token *tok2 = tok->tokAt(1)->link()->tokAt(2); tok2; tok2 = tok2->next()) {
for (const Token *tok2 = tok->next()->link()->tokAt(2); tok2; tok2 = tok2->next()) {
if (Token::simpleMatch(tok2, "if (")) {
tok2 = tok2->tokAt(1)->link()->next();
tok2 = tok2->next()->link()->next();
if (tok2->link() == NULL) {
std::ostringstream errmsg;
errmsg << "unmatched if in switch: " << tok2->linenr();
@ -599,7 +599,7 @@ void CheckOther::checkSwitchCaseFallThrough()
ifnest.push(std::make_pair(tok2->link(), false));
justbreak = false;
} else if (Token::simpleMatch(tok2, "while (")) {
tok2 = tok2->tokAt(1)->link()->next();
tok2 = tok2->next()->link()->next();
// skip over "do { } while ( ) ;" case
if (tok2->str() == "{") {
if (tok2->link() == NULL) {
@ -622,7 +622,7 @@ void CheckOther::checkSwitchCaseFallThrough()
loopnest.push(tok2->link());
justbreak = false;
} else if (Token::simpleMatch(tok2, "for (")) {
tok2 = tok2->tokAt(1)->link()->next();
tok2 = tok2->next()->link()->next();
if (tok2->link() == NULL) {
std::ostringstream errmsg;
errmsg << "unmatched for in switch: " << tok2->linenr();
@ -633,7 +633,7 @@ void CheckOther::checkSwitchCaseFallThrough()
justbreak = false;
} else if (Token::Match(tok2, switchPattern)) {
// skip over nested switch, we'll come to that soon
tok2 = tok2->tokAt(1)->link()->next()->link();
tok2 = tok2->next()->link()->next()->link();
} else if (Token::Match(tok2, breakPattern)) {
if (loopnest.empty()) {
justbreak = true;
@ -1330,7 +1330,7 @@ void CheckOther::checkComparisonOfBoolWithInt()
std::map<unsigned int, bool> boolvars; // Contains all declarated standard type variables and indicates whether its a bool or not.
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
if (Token::Match(tok, "[{};(,] %type% %var% [;=,)]") && tok->tokAt(1)->isStandardType()) { // Declaration of standard type variable
if (Token::Match(tok, "[{};(,] %type% %var% [;=,)]") && tok->next()->isStandardType()) { // Declaration of standard type variable
boolvars[tok->tokAt(2)->varId()] = (tok->strAt(1) == "bool");
} else if (Token::Match(tok, "%var% >|>=|==|!=|<=|< %num%")) { // Comparing variable with number
const Token *varTok = tok;
@ -1444,7 +1444,7 @@ void CheckOther::checkUnsignedDivision()
std::map<unsigned int, char> varsign;
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
if (Token::Match(tok, "[{};(,] %type% %var% [;=,)]")) {
if (tok->tokAt(1)->isUnsigned())
if (tok->next()->isUnsigned())
varsign[tok->tokAt(2)->varId()] = 'u';
else
varsign[tok->tokAt(2)->varId()] = 's';
@ -1452,7 +1452,7 @@ void CheckOther::checkUnsignedDivision()
else if (!Token::Match(tok, "[).]") && Token::Match(tok->next(), "%var% / %num%")) {
if (tok->strAt(3)[0] == '-') {
char sign1 = varsign[tok->tokAt(1)->varId()];
char sign1 = varsign[tok->next()->varId()];
if (sign1 == 'u') {
udivError(tok->next());
}
@ -1482,7 +1482,7 @@ void CheckOther::checkMemsetZeroBytes()
{
for (const Token* tok = _tokenizer->tokens(); tok; tok = tok->next()) {
if (Token::simpleMatch(tok, "memset (")) {
const Token* lastParamTok = tok->tokAt(1)->link()->tokAt(-1);
const Token* lastParamTok = tok->next()->link()->tokAt(-1);
if (lastParamTok->str() == "0")
memsetZeroBytesError(tok, tok->strAt(2));
}

View File

@ -261,7 +261,7 @@ void CheckStl::stlOutOfBounds()
unsigned int indent3 = 0;
// variable id for loop variable.
unsigned int numId = tok2->tokAt(1)->varId();
unsigned int numId = tok2->next()->varId();
// variable id for the container variable
unsigned int varId = tok2->tokAt(3)->varId();
@ -1189,7 +1189,7 @@ void CheckStl::uselessCalls()
if (tok->varId() == 0)
continue;
/*if (Token::Match(tok, "%var% . compare (") &&
tok->varId() == tok->linkAt(3)->tokAt(-1)->varId()) {
tok->varId() == tok->linkAt(3)->previous()->varId()) {
uselessCallsReturnValueError(tok, tok->tokAt(2));
} else */
if (Token::Match(tok, "%var% . compare|find|rfind|find_first_not_of|find_first_of|find_last_not_of|find_last_of ( %var% [,)]") &&

View File

@ -371,7 +371,7 @@ private:
reset();
givenACodeSampleToTokenize var("X< 1>2 > x1;\n");
found = si.findClosingBracket(var.tokens()->tokAt(1), t);
found = si.findClosingBracket(var.tokens()->next(), t);
ASSERT(found);
ASSERT_EQUALS(">", t->str());
ASSERT_EQUALS(var.tokens()->strAt(3), t->str());
@ -381,7 +381,7 @@ private:
reset();
givenACodeSampleToTokenize var("X < (2 < 1) > x1;\n");
found = si.findClosingBracket(var.tokens()->tokAt(1), t);
found = si.findClosingBracket(var.tokens()->next(), t);
ASSERT(!found);
}
@ -392,14 +392,14 @@ private:
ASSERT(db && db->scopeList.size() == 2 && tokenizer.getFunctionTokenByName("func"));
if (db) {
const Scope *scope = db->findFunctionScopeByToken(tokenizer.tokens()->tokAt(1));
const Scope *scope = db->findFunctionScopeByToken(tokenizer.tokens()->next());
ASSERT(scope && scope->className == "func");
const Function *function = db->findFunctionByToken(tokenizer.tokens()->tokAt(1));
const Function *function = db->findFunctionByToken(tokenizer.tokens()->next());
ASSERT(function && function->token->str() == "func");
ASSERT(function && function->token == tokenizer.tokens()->tokAt(1));
ASSERT(function && function->token == tokenizer.tokens()->next());
ASSERT(function && function->hasBody);
}
}