From 9b74d434735952b5fd093bec04b286917fee6008 Mon Sep 17 00:00:00 2001 From: Dmitry-Me Date: Wed, 23 Apr 2014 11:18:09 +0400 Subject: [PATCH] Rename local variable plus return a bit earlier on edge condition. --- lib/checkbufferoverrun.cpp | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index 9ff4eab1c..48a527bad 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -945,6 +945,12 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vectorstr() == "return") { + tok = tok->next(); + if (!tok) + return; + } + const MathLib::bigint total_size = arrayInfo.element_size() * arrayInfo.num(0); const unsigned int declarationId = arrayInfo.declarationId(); @@ -952,13 +958,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector(varname.empty() ? 0U : (varname.size() - 1) * 2U); - - if (tok->str() == "return") { - tok = tok->next(); - if (!tok) - return; - } + const unsigned char varcount = static_cast(varname.empty() ? 0U : (varname.size() - 1) * 2U); // ValueFlow array index.. if ((declarationId > 0 && Token::Match(tok, "%varid% [", declarationId)) || @@ -992,7 +992,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector 0 && ((tok->str() == "return" || (!tok->isName() && !Token::Match(tok, "[.&]"))) && Token::Match(tok->next(), "%varid% [ %num% ]", declarationId))) || (declarationId == 0 && ((tok->str() == "return" || (!tok->isName() && !Token::Match(tok, "[.&]"))) && (Token::Match(tok->next(), (varnames + " [ %num% ]").c_str()) || Token::Match(tok->next(), (varname[0] +" [ %num% ] . " + varname[1] + " [ %num% ]").c_str()))))) { std::vector indexes; - const Token *tok2 = tok->tokAt(2 + varc); + const Token *tok2 = tok->tokAt(2 + varcount); for (; Token::Match(tok2, "[ %num% ]"); tok2 = tok2->tokAt(3)) { const MathLib::bigint index = MathLib::toLongNumber(tok2->strAt(1)); indexes.push_back(index); @@ -1039,7 +1039,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector totalElements || totalIndex < 0) { - arrayIndexOutOfBoundsError(tok->tokAt(1 + varc), arrayInfo, indexes); + arrayIndexOutOfBoundsError(tok->tokAt(1 + varcount), arrayInfo, indexes); } // Is any array index out of bounds? else { @@ -1047,14 +1047,14 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector= arrayInfo.num(i)) { if (indexes.size() == 1U) { - arrayIndexOutOfBoundsError(tok->tokAt(1 + varc), arrayInfo, indexes); + arrayIndexOutOfBoundsError(tok->tokAt(1 + varcount), arrayInfo, indexes); break; // only warn about the first one } // The access is still within the memory range for the array // so it may be intentional. else if (_settings->inconclusive) { - arrayIndexOutOfBoundsError(tok->tokAt(1 + varc), arrayInfo, indexes); + arrayIndexOutOfBoundsError(tok->tokAt(1 + varcount), arrayInfo, indexes); break; // only warn about the first one } } @@ -1088,7 +1088,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector 0 && Token::Match(tok, "strcpy|strcat ( %varid% , %str% )", declarationId)) || (declarationId == 0 && Token::Match(tok, ("strcpy|strcat ( " + varnames + " , %str% )").c_str()))) { - const std::size_t len = Token::getStrLength(tok->tokAt(varc + 4)); + const std::size_t len = Token::getStrLength(tok->tokAt(varcount + 4)); if (total_size > 0 && len >= (unsigned int)total_size) { bufferOverrunError(tok, declarationId > 0 ? std::string() : varnames); continue; @@ -1113,12 +1113,12 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vectortokAt(4 + varc)); + charactersAppend += Token::getStrLength(tok2->tokAt(4 + varcount)); if (charactersAppend >= static_cast(total_size)) { bufferOverrunError(tok2); break; } - tok2 = tok2->tokAt(7 + varc); + tok2 = tok2->tokAt(7 + varcount); } } @@ -1132,9 +1132,9 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector 0 ? std::string("snprintf ( %varid% , %num% ,") : ("snprintf ( " + varnames + " , %num% ,"); if (Token::Match(tok, snprintfPattern.c_str(), declarationId)) { - const MathLib::bigint n = MathLib::toLongNumber(tok->strAt(4 + varc)); + const MathLib::bigint n = MathLib::toLongNumber(tok->strAt(4 + varcount)); if (n > total_size) - outOfBoundsError(tok->tokAt(4 + varc), "snprintf size", true, n, total_size); + outOfBoundsError(tok->tokAt(4 + varcount), "snprintf size", true, n, total_size); } // Check function call..