From 7c4b9bed9e9118401de7989aa3aed92ad68eca1b Mon Sep 17 00:00:00 2001 From: Dmitry-Me Date: Tue, 16 Sep 2014 13:34:16 +0400 Subject: [PATCH] Move declaration, run check earlier --- lib/checkbufferoverrun.cpp | 4 ++-- lib/checkunusedvar.cpp | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index 7d8d63ada..810232c57 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -890,9 +890,9 @@ void CheckBufferOverrun::checkScope(const Token *tok, const ArrayInfo &arrayInfo } // Writing data into array.. - if (Token::Match(tok, "strcpy|strcat ( %varid% , %str% )", declarationId)) { + if (total_size > 0 && Token::Match(tok, "strcpy|strcat ( %varid% , %str% )", declarationId)) { const std::size_t len = Token::getStrLength(tok->tokAt(4)); - if (total_size > 0 && len >= (unsigned int)total_size) { + if (len >= (unsigned int)total_size) { bufferOverrunError(tok, arrayInfo.varname()); continue; } diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index 68850f216..35daefd9c 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -1092,8 +1092,6 @@ void CheckUnusedVar::checkFunctionVariableUsage() it != variables.varUsage().end(); ++it) { const Variables::VariableUsage &usage = it->second; - const std::string &varname = usage._var->name(); - const Variable* var = symbolDatabase->getVariableFromVarId(it->first); // variable has been marked as unused so ignore it if (usage._var->nameToken()->isAttributeUnused() || usage._var->nameToken()->isAttributeUsed()) @@ -1105,6 +1103,9 @@ void CheckUnusedVar::checkFunctionVariableUsage() usage._type == Variables::referenceArray) continue; + const std::string &varname = usage._var->name(); + const Variable* var = symbolDatabase->getVariableFromVarId(it->first); + // variable has had memory allocated for it, but hasn't done // anything with that memory other than, perhaps, freeing it if (usage.unused() && !usage._modified && usage._allocateMemory)