gcc: fixed some more -Wsign-conversion warnings

This commit is contained in:
Daniel Marjamäki 2010-08-06 22:37:48 +02:00
parent adc47f1820
commit 12217461a2
7 changed files with 42 additions and 41 deletions

View File

@ -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<unsigned long>(index) >= arrayInfo.num[0])
{
std::list<const Token *> callstack;
callstack.push_back(&tok);
callstack.push_back(ftok);
std::vector<unsigned int> indexes;
indexes.push_back(index);
indexes.push_back(static_cast<unsigned long>(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<unsigned long>(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<unsigned long>(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<unsigned long>(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<ExecutionPathBufferOverrun *>(*it);
if (c && c->varId == varid)
c->value = MathLib::toLongNumber(value);
c->value = (unsigned long)MathLib::toLongNumber(value);
}
}

View File

@ -922,7 +922,7 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list<const Toke
if (sz > 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));
}

View File

@ -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<unsigned long>(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<long>(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<unsigned int>(line));
}
void CheckOther::uninitstringError(const Token *tok, const std::string &varname)

View File

@ -64,7 +64,7 @@ public:
struct TimerResultsData
{
std::clock_t _clocks;
unsigned int _numberOfResults;
long _numberOfResults;
TimerResultsData()
: _clocks(0)

View File

@ -65,20 +65,6 @@ double MathLib::toDoubleNumber(const std::string &str)
return ret;
}
template <typename T>
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

View File

@ -21,6 +21,7 @@
#define mathlibH
#include <string>
#include <sstream>
/// @addtogroup Core
/// @{
@ -36,7 +37,18 @@ public:
static double toDoubleNumber(const std::string & str);
template<typename T>
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);

View File

@ -1849,13 +1849,13 @@ void Tokenizer::arraySize()
}
if (Token::Match(tok2, "%any% } ;"))
tok->next()->insertToken(MathLib::toString<long>(sz));
tok->next()->insertToken(MathLib::toString<unsigned int>(sz));
}
else if (Token::Match(tok, "%var% [ ] = %str% ;"))
{
unsigned int sz = tok->strAt(4).length() - 1;
tok->next()->insertToken(MathLib::toString<long>(sz));
tok->next()->insertToken(MathLib::toString<unsigned int>(sz));
}
}
}
@ -3142,17 +3142,17 @@ void Tokenizer::simplifySizeof()
continue;
}
sizeOfVar[varId] = MathLib::toString<long>(size);
sizeOfVar[varId] = MathLib::toString<unsigned int>(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<long>(size * MathLib::toLongNumber(tok->strAt(2)));
sizeOfVar[varId] = MathLib::toString<unsigned long>(size * static_cast<unsigned long>(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<long>(size);
sizeOfVar[varId] = MathLib::toString<unsigned int>(size);
}
}
}
@ -3330,7 +3330,7 @@ void Tokenizer::simplifySizeof()
unsigned int size = sizeOfType(tok->tokAt(2));
if (size > 0)
{
tok->str(MathLib::toString<long>(size));
tok->str(MathLib::toString<unsigned int>(size));
Token::eraseTokens(tok, tok->tokAt(4));
}
}
@ -3361,7 +3361,7 @@ void Tokenizer::simplifySizeof()
if (sz > 0)
{
tok->str(MathLib::toString<long>(sz));
tok->str(MathLib::toString<unsigned int>(sz));
Token::eraseTokens(tok, tok->next()->link()->next());
}
}
@ -7653,7 +7653,7 @@ void Tokenizer::simplifyStructDecl()
{
std::string name;
name = "Anonymous" + MathLib::toString<long>(count++);
name = "Anonymous" + MathLib::toString<unsigned int>(count++);
tok1->insertToken(name.c_str());