From 1d8c7a9557d596d4c7edee801a724db7a908bce5 Mon Sep 17 00:00:00 2001 From: Jeff Garzik Date: Sun, 22 Apr 2012 13:51:16 -0400 Subject: [PATCH] Add casts for unavoidable signed/unsigned comparisons At these code sites, it is preferable to cast rather than change a variable's type. --- src/bignum.h | 4 ++-- src/bitcoinrpc.cpp | 10 ++++++---- src/main.cpp | 10 +++++----- src/main.h | 2 +- src/protocol.cpp | 2 +- src/script.cpp | 8 ++++---- src/script.h | 2 +- src/util.cpp | 2 +- 8 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/bignum.h b/src/bignum.h index b6ae32fec1..f0971e8850 100644 --- a/src/bignum.h +++ b/src/bignum.h @@ -117,9 +117,9 @@ public: { unsigned long n = BN_get_word(this); if (!BN_is_negative(this)) - return (n > std::numeric_limits::max() ? std::numeric_limits::max() : n); + return (n > (unsigned long)std::numeric_limits::max() ? std::numeric_limits::max() : n); else - return (n > std::numeric_limits::max() ? std::numeric_limits::min() : -(int)n); + return (n > (unsigned long)std::numeric_limits::max() ? std::numeric_limits::min() : -(int)n); } void setint64(int64 n) diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp index 206347faf8..2262923911 100644 --- a/src/bitcoinrpc.cpp +++ b/src/bitcoinrpc.cpp @@ -999,7 +999,7 @@ Value addmultisigaddress(const Array& params, bool fHelp) strAccount = AccountFromValue(params[2]); // Gather public keys - if (nRequired < 1 || keys.size() < nRequired) + if ((nRequired < 1) || ((int)keys.size() < nRequired)) throw runtime_error( strprintf("wrong number of keys" "(got %d, need at least %d)", keys.size(), nRequired)); @@ -1331,8 +1331,10 @@ Value listtransactions(const Array& params, bool fHelp) } // ret is newest to oldest - if (nFrom > ret.size()) nFrom = ret.size(); - if (nFrom+nCount > ret.size()) nCount = ret.size()-nFrom; + if (nFrom > (int)ret.size()) + nFrom = ret.size(); + if ((nFrom + nCount) > (int)ret.size()) + nCount = ret.size() - nFrom; Array::iterator first = ret.begin(); std::advance(first, nFrom); Array::iterator last = ret.begin(); @@ -2202,7 +2204,7 @@ int ReadHTTP(std::basic_istream& stream, map& mapHeadersRe // Read header int nLen = ReadHTTPHeader(stream, mapHeadersRet); - if (nLen < 0 || nLen > MAX_SIZE) + if (nLen < 0 || nLen > (int)MAX_SIZE) return 500; // Read message diff --git a/src/main.cpp b/src/main.cpp index 2480d26591..e5d110d8ef 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -376,10 +376,10 @@ int CMerkleTx::SetMerkleBranch(const CBlock* pblock) hashBlock = pblock->GetHash(); // Locate the transaction - for (nIndex = 0; nIndex < pblock->vtx.size(); nIndex++) + for (nIndex = 0; nIndex < (int)pblock->vtx.size(); nIndex++) if (pblock->vtx[nIndex] == *(CTransaction*)this) break; - if (nIndex == pblock->vtx.size()) + if (nIndex == (int)pblock->vtx.size()) { vMerkleBranch.clear(); nIndex = -1; @@ -2750,7 +2750,7 @@ bool ProcessMessages(CNode* pfrom) int nHeaderSize = vRecv.GetSerializeSize(CMessageHeader()); if (vRecv.end() - pstart < nHeaderSize) { - if (vRecv.size() > nHeaderSize) + if ((int)vRecv.size() > nHeaderSize) { printf("\n\nPROCESSMESSAGE MESSAGESTART NOT FOUND\n\n"); vRecv.erase(vRecv.begin(), vRecv.end() - nHeaderSize); @@ -3084,7 +3084,7 @@ unsigned int static ScanHash_CryptoPP(char* pmidstate, char* pdata, char* phash1 if ((nNonce & 0xffff) == 0) { nHashesDone = 0xffff+1; - return -1; + return (unsigned int) -1; } } } @@ -3461,7 +3461,7 @@ void static BitcoinMiner(CWallet *pwallet) (char*)&hash, nHashesDone); // Check if something found - if (nNonceFound != -1) + if (nNonceFound != (unsigned int) -1) { for (unsigned int i = 0; i < sizeof(hash)/4; i++) ((unsigned int*)&hash)[i] = ByteReverse(((unsigned int*)&hash)[i]); diff --git a/src/main.h b/src/main.h index 034911ac26..423891c3bc 100644 --- a/src/main.h +++ b/src/main.h @@ -437,7 +437,7 @@ public: nBlockHeight = nBestHeight; if (nBlockTime == 0) nBlockTime = GetAdjustedTime(); - if ((int64)nLockTime < (nLockTime < LOCKTIME_THRESHOLD ? (int64)nBlockHeight : nBlockTime)) + if ((int64)nLockTime < ((int64)nLockTime < LOCKTIME_THRESHOLD ? (int64)nBlockHeight : nBlockTime)) return true; BOOST_FOREACH(const CTxIn& txin, vin) if (!txin.IsFinal()) diff --git a/src/protocol.cpp b/src/protocol.cpp index 06306cf8e1..fda31966f2 100644 --- a/src/protocol.cpp +++ b/src/protocol.cpp @@ -128,7 +128,7 @@ bool operator<(const CInv& a, const CInv& b) bool CInv::IsKnownType() const { - return (type >= 1 && type < ARRAYLEN(ppszTypeName)); + return (type >= 1 && type < (int)ARRAYLEN(ppszTypeName)); } const char* CInv::GetCommand() const diff --git a/src/script.cpp b/src/script.cpp index eb8200bcc3..660023ef48 100644 --- a/src/script.cpp +++ b/src/script.cpp @@ -536,7 +536,7 @@ bool EvalScript(vector >& stack, const CScript& script, co return false; int n = CastToBigNum(stacktop(-1)).getint(); popstack(stack); - if (n < 0 || n >= stack.size()) + if (n < 0 || n >= (int)stack.size()) return false; valtype vch = stacktop(-n-1); if (opcode == OP_ROLL) @@ -604,9 +604,9 @@ bool EvalScript(vector >& stack, const CScript& script, co int nEnd = nBegin + CastToBigNum(stacktop(-1)).getint(); if (nBegin < 0 || nEnd < nBegin) return false; - if (nBegin > vch.size()) + if (nBegin > (int)vch.size()) nBegin = vch.size(); - if (nEnd > vch.size()) + if (nEnd > (int)vch.size()) nEnd = vch.size(); vch.erase(vch.begin() + nEnd, vch.end()); vch.erase(vch.begin(), vch.begin() + nBegin); @@ -625,7 +625,7 @@ bool EvalScript(vector >& stack, const CScript& script, co int nSize = CastToBigNum(stacktop(-1)).getint(); if (nSize < 0) return false; - if (nSize > vch.size()) + if (nSize > (int)vch.size()) nSize = vch.size(); if (opcode == OP_LEFT) vch.erase(vch.begin() + nSize, vch.end()); diff --git a/src/script.h b/src/script.h index 52922af16f..e41e09b6b3 100644 --- a/src/script.h +++ b/src/script.h @@ -467,7 +467,7 @@ public: opcodetype opcode; do { - while (end() - pc >= b.size() && memcmp(&pc[0], &b[0], b.size()) == 0) + while (end() - pc >= (long)b.size() && memcmp(&pc[0], &b[0], b.size()) == 0) { erase(pc, pc + b.size()); ++nFound; diff --git a/src/util.cpp b/src/util.cpp index 9f2de3449d..016e9deb0c 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -283,7 +283,7 @@ int my_snprintf(char* buffer, size_t limit, const char* format, ...) va_start(arg_ptr, format); int ret = _vsnprintf(buffer, limit, format, arg_ptr); va_end(arg_ptr); - if (ret < 0 || ret >= limit) + if (ret < 0 || ret >= (int)limit) { ret = limit - 1; buffer[limit-1] = 0;