diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index 520ed16a4..d896cd8ad 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -587,14 +587,14 @@ void CheckBufferOverrun::checkFunctionCall(const Token &tok, unsigned int par, c if (Token::Match(ftok->previous(), "[=+-*/;{}] %var% [ %num% ]")) { long index = MathLib::toLongNumber(ftok->strAt(2)); - if (index >= arrayInfo.num[0]) + if (index >= 0 && static_cast(index) >= arrayInfo.num[0]) { std::list callstack; callstack.push_back(&tok); callstack.push_back(ftok); std::vector indexes; - indexes.push_back(index); + indexes.push_back(static_cast(index)); arrayIndexOutOfBounds(callstack, arrayInfo, indexes); } @@ -989,16 +989,16 @@ void CheckBufferOverrun::checkScope(const Token *tok, const ArrayInfo &arrayInfo { if (tok->str() == "strncat") { - const unsigned int n = MathLib::toLongNumber(tok->strAt(6)); - if (n >= total_size) + const long n = MathLib::toLongNumber(tok->strAt(6)); + if (static_cast(n) >= total_size) strncatUsage(tok); } // Dangerous usage of strncpy + strncat.. if (Token::Match(tok->tokAt(8), "; strncat ( %varid% , %any% , %num% )", arrayInfo.varid)) { - const unsigned int n = MathLib::toLongNumber(tok->strAt(6)) + MathLib::toLongNumber(tok->strAt(15)); - if (n > total_size) + const long n = MathLib::toLongNumber(tok->strAt(6)) + MathLib::toLongNumber(tok->strAt(15)); + if (static_cast(n) > total_size) strncatUsage(tok->tokAt(9)); } } @@ -1041,8 +1041,8 @@ void CheckBufferOverrun::checkScope(const Token *tok, const ArrayInfo &arrayInfo // snprintf.. if (Token::Match(tok, "snprintf ( %varid% , %num% ,", arrayInfo.varid)) { - const unsigned int n = MathLib::toLongNumber(tok->strAt(4)); - if (n > total_size) + const long n = MathLib::toLongNumber(tok->strAt(4)); + if (static_cast(n) > total_size) outOfBounds(tok->tokAt(4), "snprintf size"); } } @@ -1759,10 +1759,13 @@ CheckBufferOverrun::ArrayInfo::ArrayInfo(unsigned int id, const std::string &nam CheckBufferOverrun::ArrayInfo CheckBufferOverrun::ArrayInfo::limit(long value) const { + unsigned long uvalue = (unsigned long)std::max(0L, value); unsigned int n = 1; for (unsigned int i = 0; i < num.size(); ++i) n *= num[i]; - return ArrayInfo(varid, varname, element_size, value > (int)n ? 0 : n - value); + if (uvalue > n) + n = uvalue; + return ArrayInfo(varid, varname, element_size, n - uvalue); } bool CheckBufferOverrun::ArrayInfo::declare(const Token *tok, const Tokenizer &tokenizer) @@ -1808,7 +1811,7 @@ bool CheckBufferOverrun::ArrayInfo::declare(const Token *tok, const Tokenizer &t while (Token::Match(atok, "%num% ] ;|=|[")) { - _num.push_back(MathLib::toLongNumber(atok->str())); + _num.push_back((unsigned long)MathLib::toLongNumber(atok->str())); atok = atok->next(); if (Token::simpleMatch(atok, "] [")) atok = atok->tokAt(2); @@ -1881,7 +1884,7 @@ private: { ExecutionPathBufferOverrun *c = dynamic_cast(*it); if (c && c->varId == varid) - c->value = MathLib::toLongNumber(value); + c->value = (unsigned long)MathLib::toLongNumber(value); } } diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index df866d463..658072e47 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -922,7 +922,7 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list 1 && Token::Match(tok->tokAt(2), "malloc ( %num% )") && - (MathLib::toLongNumber(tok->strAt(4)) % sz) != 0) + (MathLib::toLongNumber(tok->strAt(4)) % long(sz)) != 0) { mismatchSizeError(tok->tokAt(4), tok->strAt(4)); } diff --git a/lib/checkother.cpp b/lib/checkother.cpp index b13ebda6c..11d1b254b 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -3380,8 +3380,8 @@ private: if (Token::Match(tok.tokAt(6), "%num% )")) { const unsigned int len = Token::getStrLength(tok.tokAt(4)); - const unsigned int sz = MathLib::toLongNumber(tok.strAt(6)); - if (len>=sz) + const long sz = MathLib::toLongNumber(tok.strAt(6)); + if (sz >= 0 && len >= static_cast(sz)) { init_strncpy(checks, tok.tokAt(2)); return tok.next()->link(); @@ -4050,7 +4050,7 @@ void CheckOther::nullPointerError(const Token *tok, const std::string &varname) void CheckOther::nullPointerError(const Token *tok, const std::string &varname, const unsigned int line) { - reportError(tok, Severity::error, "nullPointer", "Possible null pointer dereference: " + varname + " - otherwise it is redundant to check if " + varname + " is null at line " + MathLib::toString(line)); + reportError(tok, Severity::error, "nullPointer", "Possible null pointer dereference: " + varname + " - otherwise it is redundant to check if " + varname + " is null at line " + MathLib::toString(line)); } void CheckOther::uninitstringError(const Token *tok, const std::string &varname) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 0b400438f..334af55d0 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -64,7 +64,7 @@ public: struct TimerResultsData { std::clock_t _clocks; - unsigned int _numberOfResults; + long _numberOfResults; TimerResultsData() : _clocks(0) diff --git a/lib/mathlib.cpp b/lib/mathlib.cpp index f056e63ba..57c9ad05c 100644 --- a/lib/mathlib.cpp +++ b/lib/mathlib.cpp @@ -65,20 +65,6 @@ double MathLib::toDoubleNumber(const std::string &str) return ret; } -template -std::string MathLib::toString(T d) -{ - std::ostringstream result; - result << d; - std::string strResult(result.str()); - if (strResult == "-0" - || strResult == "+0" - || strResult == "-0." - || strResult == "+0.") - return std::string("0"); - return result.str(); -} - bool MathLib::isFloat(const std::string &s) { // every number that contains a . is a float diff --git a/lib/mathlib.h b/lib/mathlib.h index 017fc46d0..1ff4f6ccd 100644 --- a/lib/mathlib.h +++ b/lib/mathlib.h @@ -21,6 +21,7 @@ #define mathlibH #include +#include /// @addtogroup Core /// @{ @@ -36,7 +37,18 @@ public: static double toDoubleNumber(const std::string & str); template - static std::string toString(T d); + static std::string toString(T d) + { + std::ostringstream result; + result << d; + std::string strResult(result.str()); + if (strResult == "-0" + || strResult == "+0" + || strResult == "-0." + || strResult == "+0.") + return std::string("0"); + return result.str(); + } static bool isInt(const std::string & str); static bool isFloat(const std::string &str); diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 9bd00fca4..38c65cbbc 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -1849,13 +1849,13 @@ void Tokenizer::arraySize() } if (Token::Match(tok2, "%any% } ;")) - tok->next()->insertToken(MathLib::toString(sz)); + tok->next()->insertToken(MathLib::toString(sz)); } else if (Token::Match(tok, "%var% [ ] = %str% ;")) { unsigned int sz = tok->strAt(4).length() - 1; - tok->next()->insertToken(MathLib::toString(sz)); + tok->next()->insertToken(MathLib::toString(sz)); } } } @@ -3142,17 +3142,17 @@ void Tokenizer::simplifySizeof() continue; } - sizeOfVar[varId] = MathLib::toString(size); + sizeOfVar[varId] = MathLib::toString(size); } else if (Token::Match(tok->tokAt(-1), "%type% %var% [ %num% ] [;=]") || Token::Match(tok->tokAt(-2), "%type% * %var% [ %num% ] [;=]")) { - unsigned int size = sizeOfType(tok->tokAt(-1)); + const unsigned int size = sizeOfType(tok->tokAt(-1)); if (size == 0) continue; - sizeOfVar[varId] = MathLib::toString(size * MathLib::toLongNumber(tok->strAt(2))); + sizeOfVar[varId] = MathLib::toString(size * static_cast(MathLib::toLongNumber(tok->strAt(2)))); } else if (Token::Match(tok->tokAt(-1), "%type% %var% [ %num% ] [,)]") || @@ -3165,11 +3165,11 @@ void Tokenizer::simplifySizeof() else if (Token::Match(tok->tokAt(-1), "%type% %var% [ ] = %str% ;")) { - unsigned int size = sizeOfType(tok->tokAt(4)); + const unsigned int size = sizeOfType(tok->tokAt(4)); if (size == 0) continue; - sizeOfVar[varId] = MathLib::toString(size); + sizeOfVar[varId] = MathLib::toString(size); } } } @@ -3330,7 +3330,7 @@ void Tokenizer::simplifySizeof() unsigned int size = sizeOfType(tok->tokAt(2)); if (size > 0) { - tok->str(MathLib::toString(size)); + tok->str(MathLib::toString(size)); Token::eraseTokens(tok, tok->tokAt(4)); } } @@ -3361,7 +3361,7 @@ void Tokenizer::simplifySizeof() if (sz > 0) { - tok->str(MathLib::toString(sz)); + tok->str(MathLib::toString(sz)); Token::eraseTokens(tok, tok->next()->link()->next()); } } @@ -7653,7 +7653,7 @@ void Tokenizer::simplifyStructDecl() { std::string name; - name = "Anonymous" + MathLib::toString(count++); + name = "Anonymous" + MathLib::toString(count++); tok1->insertToken(name.c_str());