Merge pull request #382 from Dmitry-Me/bringDeclarationsCloserToWhereTheyAreNeeded

Bring variable declarations closer to where they're first used.
This commit is contained in:
PKEuS 2014-08-02 11:12:34 +02:00
commit 6d3cb86d2a
1 changed files with 14 additions and 13 deletions

View File

@ -239,14 +239,15 @@ void CheckBufferOverrun::negativeMemoryAllocationSizeError(const Token *tok)
*/ */
static bool bailoutIfSwitch(const Token *tok, const unsigned int varid) static bool bailoutIfSwitch(const Token *tok, const unsigned int varid)
{ {
// Used later to check if the body belongs to a "if"
const bool is_if = tok->str() == "if";
const Token* end = tok->linkAt(1)->linkAt(1); const Token* end = tok->linkAt(1)->linkAt(1);
if (Token::simpleMatch(end, "} else {")) // scan the else-block if (Token::simpleMatch(end, "} else {")) // scan the else-block
end = end->linkAt(2); end = end->linkAt(2);
if (Token::simpleMatch(end, "{")) // Ticket #5203: Invalid code, bailout if (Token::simpleMatch(end, "{")) // Ticket #5203: Invalid code, bailout
return true; return true;
// Used later to check if the body belongs to a "if"
const bool is_if = tok->str() == "if";
for (; tok && tok != end; tok = tok->next()) { for (; tok && tok != end; tok = tok->next()) {
// If scanning a "if" block then bailout for "break" // If scanning a "if" block then bailout for "break"
if (is_if && (tok->str() == "break" || tok->str() == "continue")) if (is_if && (tok->str() == "break" || tok->str() == "continue"))
@ -569,13 +570,13 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<std::str
while (tok3 && Token::Match(tok3->previous(), "%var% .")) while (tok3 && Token::Match(tok3->previous(), "%var% ."))
tok3 = tok3->tokAt(-2); tok3 = tok3->tokAt(-2);
// just taking the address?
const bool addr(tok3 && (tok3->str() == "&" ||
Token::simpleMatch(tok3->previous(), "& (")));
// taking address of 1 past end? // taking address of 1 past end?
if (addr && totalIndex == totalElements) if (totalIndex == totalElements) {
const bool addr = (tok3 && (tok3->str() == "&" ||
Token::simpleMatch(tok3->previous(), "& (")));
if (addr)
continue; continue;
}
// Is totalIndex in bounds? // Is totalIndex in bounds?
if (totalIndex > totalElements || totalIndex < 0) { if (totalIndex > totalElements || totalIndex < 0) {
@ -828,10 +829,10 @@ void CheckBufferOverrun::checkScope(const Token *tok, const ArrayInfo &arrayInfo
// Check function call.. // Check function call..
checkFunctionCall(tok, arrayInfo, std::list<const Token*>()); checkFunctionCall(tok, arrayInfo, std::list<const Token*>());
if (Token::Match(tok, "strncpy|memcpy|memmove ( %varid% , %str% , %num% )", declarationId)) { if (_settings->inconclusive && Token::Match(tok, "strncpy|memcpy|memmove ( %varid% , %str% , %num% )", declarationId)) {
if (Token::getStrLength(tok->tokAt(4)) >= (unsigned int)total_size) {
const unsigned int num = (unsigned int)MathLib::toLongNumber(tok->strAt(6)); const unsigned int num = (unsigned int)MathLib::toLongNumber(tok->strAt(6));
if (Token::getStrLength(tok->tokAt(4)) >= (unsigned int)total_size && (unsigned int)total_size == num) { if ((unsigned int)total_size == num)
if (_settings->inconclusive)
bufferNotZeroTerminatedError(tok, tok->strAt(2), tok->str()); bufferNotZeroTerminatedError(tok, tok->strAt(2), tok->str());
} }
} }