From bd846c0e565ca0db276cb6b7eac7763bebe19b84 Mon Sep 17 00:00:00 2001 From: Gavin Andresen Date: Mon, 19 Dec 2011 16:27:15 -0500 Subject: [PATCH 1/3] Cleanup: removed dead code, and use C99 typedefs for int64 (supported by all modern c++ compilers) --- src/serialize.h | 8 -------- src/uint256.h | 8 -------- src/util.h | 22 +--------------------- 3 files changed, 1 insertion(+), 37 deletions(-) diff --git a/src/serialize.h b/src/serialize.h index d3f6b7d7..df060fdd 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -19,16 +19,8 @@ #include #include -#if defined(_MSC_VER) || defined(__BORLANDC__) -typedef __int64 int64; -typedef unsigned __int64 uint64; -#else typedef long long int64; typedef unsigned long long uint64; -#endif -#if defined(_MSC_VER) && _MSC_VER < 1300 -#define for if (false) ; else for -#endif #ifdef WIN32 #include diff --git a/src/uint256.h b/src/uint256.h index b6132822..d3da1f26 100644 --- a/src/uint256.h +++ b/src/uint256.h @@ -11,16 +11,8 @@ #include #include -#if defined(_MSC_VER) || defined(__BORLANDC__) -typedef __int64 int64; -typedef unsigned __int64 uint64; -#else typedef long long int64; typedef unsigned long long uint64; -#endif -#if defined(_MSC_VER) && _MSC_VER < 1300 -#define for if (false) ; else for -#endif inline int Testuint256AdHoc(std::vector vArg); diff --git a/src/util.h b/src/util.h index d9a0841a..62b21335 100644 --- a/src/util.h +++ b/src/util.h @@ -25,19 +25,8 @@ #include -#if defined(_MSC_VER) || defined(__BORLANDC__) -typedef __int64 int64; -typedef unsigned __int64 uint64; -#else typedef long long int64; typedef unsigned long long uint64; -#endif -#if defined(_MSC_VER) && _MSC_VER < 1300 -#define for if (false) ; else for -#endif -#ifndef _MSC_VER -#define __forceinline inline -#endif #define loop for (;;) #define BEGIN(a) ((char*)&(a)) @@ -53,7 +42,7 @@ typedef unsigned long long uint64; #define snprintf my_snprintf #ifndef PRI64d -#if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__MSVCRT__) +#if defined(_MSC_VER) || defined(__MSVCRT__) #define PRI64d "I64d" #define PRI64u "I64u" #define PRI64x "I64x" @@ -474,15 +463,6 @@ inline bool GetBoolArg(const std::string& strArg) -inline void heapchk() -{ -#ifdef WIN32 - /// for debugging - //if (_heapchk() != _HEAPOK) - // DebugBreak(); -#endif -} - // Randomize the stack to help protect against buffer overrun exploits #define IMPLEMENT_RANDOMIZE_STACK(ThreadFn) \ { \ From 26ce92b3526430d4a40b2faccef4facb966d6a0a Mon Sep 17 00:00:00 2001 From: Gavin Andresen Date: Mon, 19 Dec 2011 17:08:25 -0500 Subject: [PATCH 2/3] Use std::numeric_limits<> for typesafe INT_MAX/etc --- src/bignum.h | 4 ++-- src/bitcoinrpc.cpp | 8 ++++---- src/headers.h | 4 ---- src/main.cpp | 2 +- src/main.h | 12 ++++++------ src/net.cpp | 6 +++--- src/protocol.cpp | 2 +- src/qt/optionsmodel.cpp | 2 +- src/qt/transactionrecord.cpp | 2 +- src/serialize.h | 8 ++++---- src/util.cpp | 2 +- src/util.h | 6 +----- src/wallet.cpp | 2 +- 13 files changed, 26 insertions(+), 34 deletions(-) diff --git a/src/bignum.h b/src/bignum.h index f5d545f0..135eade6 100644 --- a/src/bignum.h +++ b/src/bignum.h @@ -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::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/bitcoinrpc.cpp b/src/bitcoinrpc.cpp index cd08d95d..fdb1b34a 100644 --- a/src/bitcoinrpc.cpp +++ b/src/bitcoinrpc.cpp @@ -950,7 +950,7 @@ struct tallyitem tallyitem() { nAmount = 0; - nConf = INT_MAX; + nConf = std::numeric_limits::max(); } }; @@ -1002,7 +1002,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; @@ -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::max() ? 0 : nConf))); ret.push_back(obj); } } @@ -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::max() ? 0 : nConf))); ret.push_back(obj); } } diff --git a/src/headers.h b/src/headers.h index fd086fae..b6903fe9 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 @@ -44,7 +41,6 @@ #include #include #include -#include #include #include #include diff --git a/src/main.cpp b/src/main.cpp index 052701e5..09f821b1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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::max()) return error("AcceptToMemoryPool() : not accepting nLockTime beyond 2038 yet"); // Safety limits diff --git a/src/main.h b/src/main.h index 7f8da4e8..67524ef4 100644 --- a/src/main.h +++ b/src/main.h @@ -258,17 +258,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; @@ -284,7 +284,7 @@ public: bool IsFinal() const { - return (nSequence == UINT_MAX); + return (nSequence == std::numeric_limits::max()); } friend bool operator==(const CTxIn& a, const CTxIn& b) @@ -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::max()) str += strprintf(", nSequence=%u", nSequence); str += ")"; return str; @@ -468,7 +468,7 @@ public: return false; bool fNewer = false; - unsigned int nLowest = UINT_MAX; + unsigned int nLowest = std::numeric_limits::max(); for (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 7d2f18b2..9b8dbe11 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -249,8 +249,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(); } } @@ -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::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 f46570e2..27efb8f2 100644 --- a/src/protocol.cpp +++ b/src/protocol.cpp @@ -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::max())); } unsigned char CAddress::GetByte(int n) const diff --git a/src/qt/optionsmodel.cpp b/src/qt/optionsmodel.cpp index efc216da..eef91db5 100644 --- a/src/qt/optionsmodel.cpp +++ b/src/qt/optionsmodel.cpp @@ -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::max()) { addrProxy.port = htons(nPort); walletdb.WriteSetting("addrProxy", addrProxy); diff --git a/src/qt/transactionrecord.cpp b/src/qt/transactionrecord.cpp index 77c5a012..53cd35b2 100644 --- a/src/qt/transactionrecord.cpp +++ b/src/qt/transactionrecord.cpp @@ -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::max()), (wtx.IsCoinBase() ? 1 : 0), wtx.nTimeReceived, idx); diff --git a/src/serialize.h b/src/serialize.h index df060fdd..98b69aa2 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -189,8 +189,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); } @@ -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::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 ef276e51..80ce4045 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -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::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 62b21335..9fe95e37 100644 --- a/src/util.h +++ b/src/util.h @@ -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 diff --git a/src/wallet.cpp b/src/wallet.cpp index 87f5dfd6..c6f5795c 100644 --- a/src/wallet.cpp +++ b/src/wallet.cpp @@ -782,7 +782,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; From 9ef7fa344741cb34ba4e15cff06d61d1c7a74e24 Mon Sep 17 00:00:00 2001 From: Gavin Andresen Date: Mon, 19 Dec 2011 19:04:47 -0500 Subject: [PATCH 3/3] Code cleanup: use ECDSA_size() instead of fixed 10,000 byte sig buffer, and explicity init static var --- src/key.h | 13 +++++++------ src/main.cpp | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/key.h b/src/key.h index 3f4b72d2..94ec5522 100644 --- a/src/key.h +++ b/src/key.h @@ -178,13 +178,14 @@ public: bool Sign(uint256 hash, std::vector& vchSig) { - vchSig.clear(); - unsigned char pchSig[10000]; - unsigned int nSize = 0; - if (!ECDSA_sign(0, (unsigned char*)&hash, sizeof(hash), pchSig, &nSize, pkey)) + unsigned int nSize = ECDSA_size(pkey); + vchSig.resize(nSize); // Make sure it is big enough + if (!ECDSA_sign(0, (unsigned char*)&hash, sizeof(hash), &vchSig[0], &nSize, pkey)) + { + vchSig.clear(); return false; - vchSig.resize(nSize); - memcpy(&vchSig[0], pchSig, nSize); + } + vchSig.resize(nSize); // Shrink to fit actual size return true; } diff --git a/src/main.cpp b/src/main.cpp index 09f821b1..e2f3cc9f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1875,7 +1875,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv) } // Ask the first connected node for block updates - static int nAskedForBlocks; + static int nAskedForBlocks = 0; if (!pfrom->fClient && (pfrom->nVersion < 32000 || pfrom->nVersion >= 32400) && (nAskedForBlocks < 1 || vNodes.size() <= 1))