From 5cd2a640a5ef867c376582810b1dd4b6fa43df5e Mon Sep 17 00:00:00 2001 From: Gavin Andresen Date: Mon, 19 Dec 2011 17:08:25 -0500 Subject: Use std::numeric_limits<> for typesafe INT_MAX/etc (this fixes a Mac OS X gitian build error for 0.5.x) --- src/bignum.h | 4 ++-- src/headers.h | 4 ---- src/main.cpp | 2 +- src/main.h | 12 ++++++------ src/net.cpp | 6 +++--- src/protocol.cpp | 2 +- src/rpc.cpp | 8 ++++---- src/serialize.h | 8 ++++---- src/util.cpp | 2 +- src/util.h | 6 +----- src/wallet.cpp | 2 +- 11 files changed, 24 insertions(+), 32 deletions(-) (limited to 'src') diff --git a/src/bignum.h b/src/bignum.h index bd42f77ce0..4a3fb38b00 100644 --- a/src/bignum.h +++ b/src/bignum.h @@ -116,9 +116,9 @@ public: { unsigned long n = BN_get_word(this); if (!BN_is_negative(this)) - return (n > INT_MAX ? INT_MAX : n); + return (n > std::numeric_limits::max() ? std::numeric_limits::max() : n); else - return (n > INT_MAX ? INT_MIN : -(int)n); + return (n > std::numeric_limits::max() ? std::numeric_limits::min() : -(int)n); } void setint64(int64 n) diff --git a/src/headers.h b/src/headers.h index 537d405b88..9b880c1528 100644 --- a/src/headers.h +++ b/src/headers.h @@ -21,9 +21,6 @@ // Include boost/foreach here as it defines __STDC_LIMIT_MACROS on some systems. #include -#ifndef __STDC_LIMIT_MACROS -#define __STDC_LIMIT_MACROS // to enable UINT64_MAX from stdint.h -#endif #if (defined(__unix__) || defined(unix)) && !defined(USG) #include // to get BSD define @@ -52,7 +49,6 @@ #include #include #include -#include #include #include #include diff --git a/src/main.cpp b/src/main.cpp index ee814aa552..0f45b2e16b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -372,7 +372,7 @@ bool CTransaction::AcceptToMemoryPool(CTxDB& txdb, bool fCheckInputs, bool* pfMi return error("AcceptToMemoryPool() : coinbase as individual tx"); // To help v0.1.5 clients who would see it as a negative number - if ((int64)nLockTime > INT_MAX) + if ((int64)nLockTime > std::numeric_limits::max()) return error("AcceptToMemoryPool() : not accepting nLockTime beyond 2038 yet"); // Rather not work on nonstandard transactions (unless -testnet) diff --git a/src/main.h b/src/main.h index 930f5187d9..7aa660dba7 100644 --- a/src/main.h +++ b/src/main.h @@ -253,17 +253,17 @@ public: CTxIn() { - nSequence = UINT_MAX; + nSequence = std::numeric_limits::max(); } - explicit CTxIn(COutPoint prevoutIn, CScript scriptSigIn=CScript(), unsigned int nSequenceIn=UINT_MAX) + explicit CTxIn(COutPoint prevoutIn, CScript scriptSigIn=CScript(), unsigned int nSequenceIn=std::numeric_limits::max()) { prevout = prevoutIn; scriptSig = scriptSigIn; nSequence = nSequenceIn; } - CTxIn(uint256 hashPrevTx, unsigned int nOut, CScript scriptSigIn=CScript(), unsigned int nSequenceIn=UINT_MAX) + CTxIn(uint256 hashPrevTx, unsigned int nOut, CScript scriptSigIn=CScript(), unsigned int nSequenceIn=std::numeric_limits::max()) { prevout = COutPoint(hashPrevTx, nOut); scriptSig = scriptSigIn; @@ -279,7 +279,7 @@ public: bool IsFinal() const { - return (nSequence == UINT_MAX); + return (nSequence == std::numeric_limits::max()); } friend bool operator==(const CTxIn& a, const CTxIn& b) @@ -303,7 +303,7 @@ public: str += strprintf(", coinbase %s", HexStr(scriptSig).c_str()); else str += strprintf(", scriptSig=%s", scriptSig.ToString().substr(0,24).c_str()); - if (nSequence != UINT_MAX) + if (nSequence != std::numeric_limits::max()) str += strprintf(", nSequence=%u", nSequence); str += ")"; return str; @@ -460,7 +460,7 @@ public: return false; bool fNewer = false; - unsigned int nLowest = UINT_MAX; + unsigned int nLowest = std::numeric_limits::max(); for (unsigned int i = 0; i < vin.size(); i++) { if (vin[i].nSequence != old.vin[i].nSequence) diff --git a/src/net.cpp b/src/net.cpp index a9f6c87bc7..2ff539a18e 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -251,8 +251,8 @@ bool Lookup(const char *pszName, vector& vaddr, int nServices, int nMa else pszColon[0] = 0; port = portParsed; - if (port < 0 || port > USHRT_MAX) - port = USHRT_MAX; + if (port < 0 || port > std::numeric_limits::max()) + port = std::numeric_limits::max(); } } @@ -1489,7 +1489,7 @@ void ThreadOpenConnections2(void* parg) // Choose an address to connect to based on most recently seen // CAddress addrConnect; - int64 nBest = INT64_MIN; + int64 nBest = std::numeric_limits::min(); // Only connect to one address per a.b.?.? range. // Do this here so we don't have to critsect vNodes inside mapAddresses critsect. diff --git a/src/protocol.cpp b/src/protocol.cpp index a22eba4891..8314e2bb97 100644 --- a/src/protocol.cpp +++ b/src/protocol.cpp @@ -224,7 +224,7 @@ bool CAddress::IsValid() const if (memcmp(pchReserved, pchIPv4+3, sizeof(pchIPv4)-3) == 0) return false; - return (ip != 0 && ip != INADDR_NONE && port != htons(USHRT_MAX)); + return (ip != 0 && ip != INADDR_NONE && port != htons(std::numeric_limits::max())); } unsigned char CAddress::GetByte(int n) const diff --git a/src/rpc.cpp b/src/rpc.cpp index acd7a8933a..786bf2987a 100644 --- a/src/rpc.cpp +++ b/src/rpc.cpp @@ -875,7 +875,7 @@ struct tallyitem tallyitem() { nAmount = 0; - nConf = INT_MAX; + nConf = std::numeric_limits::max(); } }; @@ -927,7 +927,7 @@ Value ListReceived(const Array& params, bool fByAccounts) continue; int64 nAmount = 0; - int nConf = INT_MAX; + int nConf = std::numeric_limits::max(); if (it != mapTally.end()) { nAmount = (*it).second.nAmount; @@ -947,7 +947,7 @@ Value ListReceived(const Array& params, bool fByAccounts) obj.push_back(Pair("account", strAccount)); obj.push_back(Pair("label", strAccount)); // deprecated obj.push_back(Pair("amount", ValueFromAmount(nAmount))); - obj.push_back(Pair("confirmations", (nConf == INT_MAX ? 0 : nConf))); + obj.push_back(Pair("confirmations", (nConf == std::numeric_limits::max() ? 0 : nConf))); ret.push_back(obj); } } @@ -962,7 +962,7 @@ Value ListReceived(const Array& params, bool fByAccounts) obj.push_back(Pair("account", (*it).first)); obj.push_back(Pair("label", (*it).first)); // deprecated obj.push_back(Pair("amount", ValueFromAmount(nAmount))); - obj.push_back(Pair("confirmations", (nConf == INT_MAX ? 0 : nConf))); + obj.push_back(Pair("confirmations", (nConf == std::numeric_limits::max() ? 0 : nConf))); ret.push_back(obj); } } diff --git a/src/serialize.h b/src/serialize.h index db3c963654..7b5aac24e3 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -201,8 +201,8 @@ template inline void Unserialize(Stream& s, bool& a, int, int=0 inline unsigned int GetSizeOfCompactSize(uint64 nSize) { if (nSize < 253) return sizeof(unsigned char); - else if (nSize <= USHRT_MAX) return sizeof(unsigned char) + sizeof(unsigned short); - else if (nSize <= UINT_MAX) return sizeof(unsigned char) + sizeof(unsigned int); + else if (nSize <= std::numeric_limits::max()) return sizeof(unsigned char) + sizeof(unsigned short); + else if (nSize <= std::numeric_limits::max()) return sizeof(unsigned char) + sizeof(unsigned int); else return sizeof(unsigned char) + sizeof(uint64); } @@ -214,14 +214,14 @@ void WriteCompactSize(Stream& os, uint64 nSize) unsigned char chSize = nSize; WRITEDATA(os, chSize); } - else if (nSize <= USHRT_MAX) + else if (nSize <= std::numeric_limits::max()) { unsigned char chSize = 253; unsigned short xSize = nSize; WRITEDATA(os, chSize); WRITEDATA(os, xSize); } - else if (nSize <= UINT_MAX) + else if (nSize <= std::numeric_limits::max()) { unsigned char chSize = 254; unsigned int xSize = nSize; diff --git a/src/util.cpp b/src/util.cpp index ccd39aa229..cae01dffe6 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -144,7 +144,7 @@ uint64 GetRand(uint64 nMax) // The range of the random source must be a multiple of the modulus // to give every possible output value an equal possibility - uint64 nRange = (UINT64_MAX / nMax) * nMax; + uint64 nRange = (std::numeric_limits::max() / nMax) * nMax; uint64 nRand = 0; do RAND_bytes((unsigned char*)&nRand, sizeof(nRand)); diff --git a/src/util.h b/src/util.h index 88cc1e79ea..284cf33c4b 100644 --- a/src/util.h +++ b/src/util.h @@ -84,11 +84,7 @@ T* alignup(T* p) #ifdef __WXMSW__ #define MSG_NOSIGNAL 0 #define MSG_DONTWAIT 0 -#ifndef UINT64_MAX -#define UINT64_MAX _UI64_MAX -#define INT64_MAX _I64_MAX -#define INT64_MIN _I64_MIN -#endif + #ifndef S_IRUSR #define S_IRUSR 0400 #define S_IWUSR 0200 diff --git a/src/wallet.cpp b/src/wallet.cpp index 973727b201..8fe77119ee 100644 --- a/src/wallet.cpp +++ b/src/wallet.cpp @@ -747,7 +747,7 @@ bool CWallet::SelectCoinsMinConf(int64 nTargetValue, int nConfMine, int nConfThe // List of values less than target pair > coinLowestLarger; - coinLowestLarger.first = INT64_MAX; + coinLowestLarger.first = std::numeric_limits::max(); coinLowestLarger.second.first = NULL; vector > > vValue; int64 nTotalLower = 0; -- cgit v1.2.3