From 9b38ae73c1bb82eb3117a64085fc789a21919abc Mon Sep 17 00:00:00 2001 From: amai2012 Date: Mon, 7 Jul 2014 21:25:30 +0200 Subject: [PATCH] Attempt to fix 2 Coverity messages. Replace a few unsigned int by std::size_t --- cli/cppcheckexecutor.cpp | 24 ++++++++++++++---------- lib/checkbufferoverrun.cpp | 16 ++++++++-------- lib/checkclass.cpp | 8 ++++---- lib/checkio.cpp | 2 +- lib/preprocessor.cpp | 10 +++++----- lib/templatesimplifier.cpp | 2 +- lib/tokenlist.cpp | 4 ++-- lib/valueflow.cpp | 2 +- 8 files changed, 36 insertions(+), 32 deletions(-) diff --git a/cli/cppcheckexecutor.cpp b/cli/cppcheckexecutor.cpp index 7c7104543..83b91b205 100644 --- a/cli/cppcheckexecutor.cpp +++ b/cli/cppcheckexecutor.cpp @@ -451,19 +451,20 @@ static fpSymFunctionTableAccess64 pSymFunctionTableAccess64; typedef BOOL (WINAPI *fpSymInitialize)(HANDLE, PCSTR, BOOL); static fpSymInitialize pSymInitialize; +static HMODULE hLibDbgHelp; // avoid explicit dependency on Dbghelp.dll static bool loadDbgHelp() { - HMODULE hLib = ::LoadLibraryW(L"Dbghelp.dll"); - if (!hLib) + hLibDbgHelp = ::LoadLibraryW(L"Dbghelp.dll"); + if (!hLibDbgHelp) return true; - pStackWalk64 = (fpStackWalk64) ::GetProcAddress(hLib, "StackWalk64"); - pSymGetModuleBase64 = (fpSymGetModuleBase64) ::GetProcAddress(hLib, "SymGetModuleBase64"); - pSymGetSymFromAddr64 = (fpSymGetSymFromAddr64) ::GetProcAddress(hLib, "SymGetSymFromAddr64"); - pSymGetLineFromAddr64 = (fpSymGetLineFromAddr64)::GetProcAddress(hLib, "SymGetLineFromAddr64"); - pSymFunctionTableAccess64 = (fpSymFunctionTableAccess64)::GetProcAddress(hLib, "SymFunctionTableAccess64"); - pSymInitialize = (fpSymInitialize) ::GetProcAddress(hLib, "SymInitialize"); - pUnDecorateSymbolName = (fpUnDecorateSymbolName)::GetProcAddress(hLib, "UnDecorateSymbolName"); + pStackWalk64 = (fpStackWalk64) ::GetProcAddress(hLibDbgHelp, "StackWalk64"); + pSymGetModuleBase64 = (fpSymGetModuleBase64) ::GetProcAddress(hLibDbgHelp, "SymGetModuleBase64"); + pSymGetSymFromAddr64 = (fpSymGetSymFromAddr64) ::GetProcAddress(hLibDbgHelp, "SymGetSymFromAddr64"); + pSymGetLineFromAddr64 = (fpSymGetLineFromAddr64)::GetProcAddress(hLibDbgHelp, "SymGetLineFromAddr64"); + pSymFunctionTableAccess64 = (fpSymFunctionTableAccess64)::GetProcAddress(hLibDbgHelp, "SymFunctionTableAccess64"); + pSymInitialize = (fpSymInitialize) ::GetProcAddress(hLibDbgHelp, "SymInitialize"); + pUnDecorateSymbolName = (fpUnDecorateSymbolName)::GetProcAddress(hLibDbgHelp, "UnDecorateSymbolName"); return true; } @@ -520,7 +521,7 @@ static void PrintCallstack(FILE* f, PEXCEPTION_POINTERS ex) ); if (!result) // official end... break; - result = pSymGetSymFromAddr64(hProcess, (ULONG64)stack.AddrPC.Offset, &displacement, &symbol); + pSymGetSymFromAddr64(hProcess, (ULONG64)stack.AddrPC.Offset, &displacement, &symbol); TCHAR undname[maxnamelength]= {0}; pUnDecorateSymbolName((const TCHAR*)symbol.Name, (PTSTR)undname, (DWORD)GetArrayLength(undname), UNDNAME_COMPLETE); if (beyond_main>=0) @@ -535,6 +536,9 @@ static void PrintCallstack(FILE* f, PEXCEPTION_POINTERS ex) if (0==stack.AddrReturn.Offset || beyond_main>2) // StackWalk64() sometimes doesn't reach any end... break; } + + FreeLibrary(hLibDbgHelp); + hLibDbgHelp=0; } /* diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index fec37b027..ced95a8a6 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -491,7 +491,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector(varname.empty() ? 0U : (varname.size() - 1) * 2U); @@ -553,8 +553,8 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector= arrayInfo.num(i)) { if (indexes.size() == 1U) { arrayIndexOutOfBoundsError(tok->tokAt(1 + varcount), arrayInfo, indexes); @@ -752,8 +752,8 @@ void CheckBufferOverrun::valueFlowCheckArrayIndex(const Token * const tok, const MathLib::bigint totalIndex = 0; // calculate the totalElements and totalIndex.. - for (unsigned int i = 0; i < indexes.size(); ++i) { - std::size_t ri = indexes.size() - 1 - i; + for (std::size_t i = 0; i < indexes.size(); ++i) { + const std::size_t ri = indexes.size() - 1 - i; totalIndex += indexes[ri].intvalue * totalElements; totalElements *= arrayInfo.num(ri); } @@ -775,7 +775,7 @@ void CheckBufferOverrun::valueFlowCheckArrayIndex(const Token * const tok, const // Is any array index out of bounds? else { // check each index for overflow - for (unsigned int i = 0; i < indexes.size(); ++i) { + for (std::size_t i = 0; i < indexes.size(); ++i) { if (indexes[i].intvalue >= arrayInfo.num(i)) { // The access is still within the memory range for the array // so it may be intentional. @@ -1399,7 +1399,7 @@ MathLib::bigint CheckBufferOverrun::countSprintfLength(const std::string &input_ if (digits_string.find('.') != std::string::npos) { const std::string endStr = digits_string.substr(digits_string.find('.') + 1); - unsigned int maxLen = std::max(static_cast(std::abs(std::atoi(endStr.c_str()))), 1U); + const unsigned int maxLen = std::max(static_cast(std::abs(std::atoi(endStr.c_str()))), 1U); if (input_string[i] == 's') { // For strings, the length after the dot "%.2s" will limit diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 05a0b2d7b..c40107b07 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -1422,7 +1422,7 @@ void CheckClass::virtualDestructor() const Token *derivedClass = derived->next(); // Iterate through each base class... - for (unsigned int j = 0; j < scope->definedType->derivedFrom.size(); ++j) { + for (std::size_t j = 0; j < scope->definedType->derivedFrom.size(); ++j) { // Check if base class is public and exists in database if (scope->definedType->derivedFrom[j].access != Private && scope->definedType->derivedFrom[j].type) { const Type *derivedFrom = scope->definedType->derivedFrom[j].type; @@ -1680,7 +1680,7 @@ bool CheckClass::isMemberVar(const Scope *scope, const Token *tok) const // not found in this class if (!scope->definedType->derivedFrom.empty() && !scope->definedType->hasCircularDependencies()) { // check each base class - for (unsigned int i = 0; i < scope->definedType->derivedFrom.size(); ++i) { + for (std::size_t i = 0; i < scope->definedType->derivedFrom.size(); ++i) { // find the base class const Type *derivedFrom = scope->definedType->derivedFrom[i].type; @@ -1746,7 +1746,7 @@ bool CheckClass::isMemberFunc(const Scope *scope, const Token *tok) const // not found in this class if (!scope->definedType->derivedFrom.empty()) { // check each base class - for (unsigned int i = 0; i < scope->definedType->derivedFrom.size(); ++i) { + for (std::size_t i = 0; i < scope->definedType->derivedFrom.size(); ++i) { // find the base class const Type *derivedFrom = scope->definedType->derivedFrom[i].type; @@ -1786,7 +1786,7 @@ bool CheckClass::isConstMemberFunc(const Scope *scope, const Token *tok) const // not found in this class if (!scope->definedType->derivedFrom.empty()) { // check each base class - for (unsigned int i = 0; i < scope->definedType->derivedFrom.size(); ++i) { + for (std::size_t i = 0; i < scope->definedType->derivedFrom.size(); ++i) { // find the base class const Type *derivedFrom = scope->definedType->derivedFrom[i].type; diff --git a/lib/checkio.cpp b/lib/checkio.cpp index 2a5956c14..3adc389b4 100644 --- a/lib/checkio.cpp +++ b/lib/checkio.cpp @@ -363,7 +363,7 @@ void CheckIO::invalidScanf() // scan the string backwards, so we do not need to keep states const std::string &formatstr(formatToken->str()); - for (unsigned int i = 1; i < formatstr.length(); i++) { + for (std::size_t i = 1; i < formatstr.length(); i++) { if (formatstr[i] == '%') format = !format; diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index c4b450ada..0e06c09ca 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -871,7 +871,7 @@ std::string Preprocessor::removeSpaceNearNL(const std::string &str) { std::string tmp; char prev = 0; - for (unsigned int i = 0; i < str.size(); i++) { + for (std::size_t i = 0; i < str.size(); i++) { if (str[i] == ' ' && ((i > 0 && prev == '\n') || (i + 1 < str.size() && str[i+1] == '\n') @@ -2569,7 +2569,7 @@ private: std::vector params2(params1); - for (unsigned int ipar = 0; ipar < params1.size(); ++ipar) { + for (std::size_t ipar = 0; ipar < params1.size(); ++ipar) { const std::string s(innerMacroName + "("); std::string param(params1[ipar]); if (param.compare(0,s.length(),s)==0 && param[param.length()-1]==')') { @@ -2695,7 +2695,7 @@ public: // Replace "__VA_ARGS__" with parameters if (!_nopar) { std::string s; - for (unsigned int i = 0; i < params2.size(); ++i) { + for (std::size_t i = 0; i < params2.size(); ++i) { if (i > 0) s += ","; s += params2[i]; @@ -2740,13 +2740,13 @@ public: if (stringify) { str = str.erase(0, 1); } - for (unsigned int i = 0; i < _params.size(); ++i) { + for (std::size_t i = 0; i < _params.size(); ++i) { if (str == _params[i]) { if (_variadic && (i == _params.size() - 1 || (givenparams.size() + 2 == _params.size() && i + 1 == _params.size() - 1))) { str = ""; - for (unsigned int j = (unsigned int)_params.size() - 1; j < givenparams.size(); ++j) { + for (std::size_t j = _params.size() - 1; j < givenparams.size(); ++j) { if (optcomma || j > _params.size() - 1) str += ","; optcomma = false; diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index 0f71f99fa..6e2c68eba 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -51,7 +51,7 @@ static void printlist(const std::list &list) static void printvector(const std::vector &v) { - for (unsigned int i = 0; i < v.size(); i++) { + for (std::size_t i = 0; i < v.size(); i++) { const Token *token = v[i]; std::cout << " " << i << ":"; while (token && !Token::Match(token, "[{};]")) { diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index 847d0b578..6c697722a 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -75,7 +75,7 @@ void TokenList::deallocateTokens() unsigned int TokenList::appendFileIfNew(const std::string &fileName) { // Has this file been tokenized already? - for (unsigned int i = 0; i < _files.size(); ++i) + for (std::size_t i = 0; i < _files.size(); ++i) if (Path::sameFileName(_files[i], fileName)) return i; @@ -391,7 +391,7 @@ bool TokenList::createTokens(std::istream &code, const std::string& file0) _back->isExpandedMacro(expandedMacro); Token::assignProgressValues(_front); - for (unsigned int i = 1; i < _files.size(); i++) + for (std::size_t i = 1; i < _files.size(); i++) _files[i] = Path::getRelativePath(_files[i], _settings->_basePaths); return true; diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 047a9e66f..d38996310 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -1337,7 +1337,7 @@ static void valueFlowFunctionReturn(TokenList *tokenlist, ErrorLogger *errorLogg } std::map programMemory; - for (unsigned int i = 0; i < parvalues.size(); ++i) { + for (std::size_t i = 0; i < parvalues.size(); ++i) { const Variable * const arg = function->getArgumentVar(i); if (!arg || !Token::Match(arg->typeStartToken(), "%type% %var% ,|)")) { if (settings->debugwarnings)