Browse Source

Use std::numeric_limits<> for typesafe INT_MAX/etc

0.8
Gavin Andresen 13 years ago
parent
commit
26ce92b352
  1. 4
      src/bignum.h
  2. 8
      src/bitcoinrpc.cpp
  3. 4
      src/headers.h
  4. 2
      src/main.cpp
  5. 12
      src/main.h
  6. 6
      src/net.cpp
  7. 2
      src/protocol.cpp
  8. 2
      src/qt/optionsmodel.cpp
  9. 2
      src/qt/transactionrecord.cpp
  10. 8
      src/serialize.h
  11. 2
      src/util.cpp
  12. 6
      src/util.h
  13. 2
      src/wallet.cpp

4
src/bignum.h

@ -115,9 +115,9 @@ public: @@ -115,9 +115,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<int>::max() ? std::numeric_limits<int>::max() : n);
else
return (n > INT_MAX ? INT_MIN : -(int)n);
return (n > std::numeric_limits<int>::max() ? std::numeric_limits<int>::min() : -(int)n);
}
void setint64(int64 n)

8
src/bitcoinrpc.cpp

@ -950,7 +950,7 @@ struct tallyitem @@ -950,7 +950,7 @@ struct tallyitem
tallyitem()
{
nAmount = 0;
nConf = INT_MAX;
nConf = std::numeric_limits<int>::max();
}
};
@ -1002,7 +1002,7 @@ Value ListReceived(const Array& params, bool fByAccounts) @@ -1002,7 +1002,7 @@ Value ListReceived(const Array& params, bool fByAccounts)
continue;
int64 nAmount = 0;
int nConf = INT_MAX;
int nConf = std::numeric_limits<int>::max();
if (it != mapTally.end())
{
nAmount = (*it).second.nAmount;
@ -1021,7 +1021,7 @@ Value ListReceived(const Array& params, bool fByAccounts) @@ -1021,7 +1021,7 @@ Value ListReceived(const Array& params, bool fByAccounts)
obj.push_back(Pair("address", address.ToString()));
obj.push_back(Pair("account", strAccount));
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<int>::max() ? 0 : nConf)));
ret.push_back(obj);
}
}
@ -1035,7 +1035,7 @@ Value ListReceived(const Array& params, bool fByAccounts) @@ -1035,7 +1035,7 @@ Value ListReceived(const Array& params, bool fByAccounts)
Object obj;
obj.push_back(Pair("account", (*it).first));
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<int>::max() ? 0 : nConf)));
ret.push_back(obj);
}
}

4
src/headers.h

@ -21,9 +21,6 @@ @@ -21,9 +21,6 @@
// Include boost/foreach here as it defines __STDC_LIMIT_MACROS on some systems.
#include <boost/foreach.hpp>
#ifndef __STDC_LIMIT_MACROS
#define __STDC_LIMIT_MACROS // to enable UINT64_MAX from stdint.h
#endif
#if (defined(__unix__) || defined(unix)) && !defined(USG)
#include <sys/param.h> // to get BSD define
@ -44,7 +41,6 @@ @@ -44,7 +41,6 @@
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <limits.h>
#include <float.h>
#include <assert.h>
#include <iostream>

2
src/main.cpp

@ -366,7 +366,7 @@ bool CTransaction::AcceptToMemoryPool(CTxDB& txdb, bool fCheckInputs, bool* pfMi @@ -366,7 +366,7 @@ bool CTransaction::AcceptToMemoryPool(CTxDB& txdb, bool fCheckInputs, bool* pfMi
return DoS(100, 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<int>::max())
return error("AcceptToMemoryPool() : not accepting nLockTime beyond 2038 yet");
// Safety limits

12
src/main.h

@ -258,17 +258,17 @@ public: @@ -258,17 +258,17 @@ public:
CTxIn()
{
nSequence = UINT_MAX;
nSequence = std::numeric_limits<unsigned int>::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<unsigned int>::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<unsigned int>::max())
{
prevout = COutPoint(hashPrevTx, nOut);
scriptSig = scriptSigIn;
@ -284,7 +284,7 @@ public: @@ -284,7 +284,7 @@ public:
bool IsFinal() const
{
return (nSequence == UINT_MAX);
return (nSequence == std::numeric_limits<unsigned int>::max());
}
friend bool operator==(const CTxIn& a, const CTxIn& b)
@ -308,7 +308,7 @@ public: @@ -308,7 +308,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<unsigned int>::max())
str += strprintf(", nSequence=%u", nSequence);
str += ")";
return str;
@ -468,7 +468,7 @@ public: @@ -468,7 +468,7 @@ public:
return false;
bool fNewer = false;
unsigned int nLowest = UINT_MAX;
unsigned int nLowest = std::numeric_limits<unsigned int>::max();
for (int i = 0; i < vin.size(); i++)
{
if (vin[i].nSequence != old.vin[i].nSequence)

6
src/net.cpp

@ -249,8 +249,8 @@ bool Lookup(const char *pszName, vector<CAddress>& vaddr, int nServices, int nMa @@ -249,8 +249,8 @@ bool Lookup(const char *pszName, vector<CAddress>& 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<unsigned short>::max())
port = std::numeric_limits<unsigned short>::max();
}
}
@ -1488,7 +1488,7 @@ void ThreadOpenConnections2(void* parg) @@ -1488,7 +1488,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<int64>::min();
// Only connect to one address per a.b.?.? range.
// Do this here so we don't have to critsect vNodes inside mapAddresses critsect.

2
src/protocol.cpp

@ -223,7 +223,7 @@ bool CAddress::IsValid() const @@ -223,7 +223,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<unsigned short>::max()));
}
unsigned char CAddress::GetByte(int n) const

2
src/qt/optionsmodel.cpp

@ -101,7 +101,7 @@ bool OptionsModel::setData(const QModelIndex & index, const QVariant & value, in @@ -101,7 +101,7 @@ bool OptionsModel::setData(const QModelIndex & index, const QVariant & value, in
case ProxyPort:
{
int nPort = atoi(value.toString().toAscii().data());
if (nPort > 0 && nPort < USHRT_MAX)
if (nPort > 0 && nPort < std::numeric_limits<unsigned short>::max())
{
addrProxy.port = htons(nPort);
walletdb.WriteSetting("addrProxy", addrProxy);

2
src/qt/transactionrecord.cpp

@ -187,7 +187,7 @@ void TransactionRecord::updateStatus(const CWalletTx &wtx) @@ -187,7 +187,7 @@ void TransactionRecord::updateStatus(const CWalletTx &wtx)
// Sort order, unrecorded transactions sort to the top
status.sortKey = strprintf("%010d-%01d-%010u-%03d",
(pindex ? pindex->nHeight : INT_MAX),
(pindex ? pindex->nHeight : std::numeric_limits<int>::max()),
(wtx.IsCoinBase() ? 1 : 0),
wtx.nTimeReceived,
idx);

8
src/serialize.h

@ -189,8 +189,8 @@ template<typename Stream> inline void Unserialize(Stream& s, bool& a, int, int=0 @@ -189,8 +189,8 @@ template<typename Stream> 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<unsigned short>::max()) return sizeof(unsigned char) + sizeof(unsigned short);
else if (nSize <= std::numeric_limits<unsigned int>::max()) return sizeof(unsigned char) + sizeof(unsigned int);
else return sizeof(unsigned char) + sizeof(uint64);
}
@ -202,14 +202,14 @@ void WriteCompactSize(Stream& os, uint64 nSize) @@ -202,14 +202,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<unsigned short>::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<unsigned int>::max())
{
unsigned char chSize = 254;
unsigned int xSize = nSize;

2
src/util.cpp

@ -133,7 +133,7 @@ uint64 GetRand(uint64 nMax) @@ -133,7 +133,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<uint64>::max() / nMax) * nMax;
uint64 nRand = 0;
do
RAND_bytes((unsigned char*)&nRand, sizeof(nRand));

6
src/util.h

@ -73,11 +73,7 @@ T* alignup(T* p) @@ -73,11 +73,7 @@ T* alignup(T* p)
#ifdef WIN32
#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

2
src/wallet.cpp

@ -782,7 +782,7 @@ bool CWallet::SelectCoinsMinConf(int64 nTargetValue, int nConfMine, int nConfThe @@ -782,7 +782,7 @@ bool CWallet::SelectCoinsMinConf(int64 nTargetValue, int nConfMine, int nConfThe
// List of values less than target
pair<int64, pair<const CWalletTx*,unsigned int> > coinLowestLarger;
coinLowestLarger.first = INT64_MAX;
coinLowestLarger.first = std::numeric_limits<int64>::max();
coinLowestLarger.second.first = NULL;
vector<pair<int64, pair<const CWalletTx*,unsigned int> > > vValue;
int64 nTotalLower = 0;

Loading…
Cancel
Save