From 508e9394d3e5bfc128cfa17ad95ba6141eca1fc1 Mon Sep 17 00:00:00 2001 From: PKEuS Date: Mon, 17 Sep 2012 15:22:51 +0200 Subject: [PATCH] Added some nullpointer-checks and removed some redundant ones based on VS2012 code analysis results. --- lib/checkbufferoverrun.cpp | 13 ++++++------- lib/checkclass.cpp | 3 --- lib/checkleakautovar.cpp | 4 ++-- lib/checkmemoryleak.cpp | 4 ++-- lib/checknullpointer.cpp | 2 +- lib/checkother.cpp | 2 +- lib/checkuninitvar.cpp | 2 +- 7 files changed, 13 insertions(+), 17 deletions(-) diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index 2fb958b77..bc09e63dc 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -800,7 +800,7 @@ void CheckBufferOverrun::arrayIndexInForLoop(const Token *tok, const ArrayInfo & } if (max_value > size) { - if (tok3->strAt(1) == ")") { + if (tok3 && tok3->strAt(1) == ")") { bool usedInArray = false; for (const Token *loopTok = tok3->tokAt(2); loopTok->str() != "}" ; loopTok = loopTok->next()) { if (loopTok->varId() == arrayInfo.varid() && loopTok->tokAt(2)->varId() == counter_varid) @@ -834,7 +834,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector(varname.empty() ? 0U : (varname.size() - 1) * 2U); - if (tok && tok->str() == "return") { + if (tok->str() == "return") { tok = tok->next(); if (!tok) return; @@ -989,7 +989,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vectortokAt(4 + varc)); if (charactersAppend >= static_cast(total_size)) { bufferOverrunError(tok2); @@ -1718,8 +1718,7 @@ void CheckBufferOverrun::checkSprintfCall(const Token *tok, const MathLib::bigin //--------------------------------------------------------------------------- void CheckBufferOverrun::checkBufferAllocatedWithStrlen() { - const char pattern[] = "%var% = new|malloc|g_malloc|g_try_malloc|realloc|g_realloc|g_try_realloc"; - for (const Token *tok = Token::findmatch(_tokenizer->tokens(), pattern); tok; tok = Token::findmatch(tok->next(),pattern)) { + for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { unsigned int dstVarId; unsigned int srcVarId; @@ -1754,9 +1753,9 @@ void CheckBufferOverrun::checkBufferAllocatedWithStrlen() tok->strAt(4).find("%s") != std::string::npos) { bufferOverrunError(tok); } - } - + if (!tok) + return; } } diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 4bf1f111f..54827b067 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -332,9 +332,6 @@ void CheckClass::initializeVarList(const Function &func, std::list int level = 0; for (; ftok != func.functionScope->classEnd; ftok = ftok->next()) { - if (!ftok->next()) - break; - // Class constructor.. initializing variables like this // clKalle::clKalle() : var(value) { } if (initList) { diff --git a/lib/checkleakautovar.cpp b/lib/checkleakautovar.cpp index 7967b5586..c76818c0d 100644 --- a/lib/checkleakautovar.cpp +++ b/lib/checkleakautovar.cpp @@ -260,13 +260,13 @@ void CheckLeakAutoVar::checkScope(const Token * const startToken, if (!Token::Match(tok, "[;{}]") || Token::Match(tok->next(), "[;{}]")) continue; tok = tok->next(); - if (tok == endToken) + if (!tok || tok == endToken) break; // parse statement // assignment.. - if (tok && tok->varId() && Token::Match(tok, "%var% =")) { + if (tok->varId() && Token::Match(tok, "%var% =")) { // taking address of another variable.. if (Token::Match(tok->next(), "= %var% [+;]")) { if (tok->tokAt(2)->varId() != tok->varId()) { diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 8e818af9b..30b9bc3c5 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -2095,7 +2095,7 @@ void CheckMemoryLeakInFunction::checkScope(const Token *Tok1, const std::string noerr |= Token::simpleMatch(first, "alloc ; if return ; dealloc; }"); // Unhandled case.. - if (! noerr) { + if (!noerr && tok) { std::ostringstream errmsg; errmsg << "inconclusive leak of " << varname << ": "; errmsg << tok->stringifyList(false, false, false, false, false, 0, 0); @@ -2282,7 +2282,7 @@ void CheckMemoryLeakInClass::check() const Token *tok = var->typeStartToken(); if (tok->str() == "const") tok = tok->next(); - if (tok && tok->isStandardType()) { + if (tok->isStandardType()) { if (var->isPrivate()) checkPublicFunctions(&(*scope), var->nameToken()); diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index e6cbcc9d0..ee2d9abc3 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -759,7 +759,7 @@ void CheckNullPointer::nullPointerByDeRefAndChec() tok2 = tok2->previous(); } } - if (Token::Match(tok2, "[?:]") || tok2->varId() == varid) + if (!tok2 || Token::Match(tok2, "[?:]") || tok2->varId() == varid) continue; // unknown : this is set by isPointerDeRef if it is diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 864ec4fde..7a7667ce3 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -246,7 +246,7 @@ void CheckOther::clarifyStatement() if (Token::Match(tok2, "[{};]")) { tok = tok->tokAt(2); - while (tok) { + for (;;) { if (tok->str() == "[") tok = tok->link()->next(); diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index b8862678b..6189c7581 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1232,7 +1232,7 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var return true; // variable is seen.. - if (tok && tok->varId() == var.varId()) { + if (tok->varId() == var.varId()) { // Use variable if (!suppressErrors && isVariableUsage(tok, var.isPointer())) uninitvarError(tok, tok->str());