From e85e19be06c59529bfda100729e8ef3148349952 Mon Sep 17 00:00:00 2001 From: Thomas Holenstein Date: Sat, 21 Dec 2013 16:50:49 +0100 Subject: [PATCH] Changed Get64(.) to GetLow64() The function Get64(.) has a bug in case the width is not divisible by 64. Since it is only ever used as Get64(0) this simply changes it to this special case. Additionally, an assert is added, and a cast to prevent a compiler error. --- src/addrman.cpp | 8 ++++---- src/uint256.h | 7 ++++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/addrman.cpp b/src/addrman.cpp index 815da07c9b7..46b4a949380 100644 --- a/src/addrman.cpp +++ b/src/addrman.cpp @@ -14,12 +14,12 @@ int CAddrInfo::GetTriedBucket(const std::vector &nKey) const CDataStream ss1(SER_GETHASH, 0); std::vector vchKey = GetKey(); ss1 << nKey << vchKey; - uint64_t hash1 = Hash(ss1.begin(), ss1.end()).Get64(); + uint64_t hash1 = Hash(ss1.begin(), ss1.end()).GetLow64(); CDataStream ss2(SER_GETHASH, 0); std::vector vchGroupKey = GetGroup(); ss2 << nKey << vchGroupKey << (hash1 % ADDRMAN_TRIED_BUCKETS_PER_GROUP); - uint64_t hash2 = Hash(ss2.begin(), ss2.end()).Get64(); + uint64_t hash2 = Hash(ss2.begin(), ss2.end()).GetLow64(); return hash2 % ADDRMAN_TRIED_BUCKET_COUNT; } @@ -29,11 +29,11 @@ int CAddrInfo::GetNewBucket(const std::vector &nKey, const CNetAd std::vector vchGroupKey = GetGroup(); std::vector vchSourceGroupKey = src.GetGroup(); ss1 << nKey << vchGroupKey << vchSourceGroupKey; - uint64_t hash1 = Hash(ss1.begin(), ss1.end()).Get64(); + uint64_t hash1 = Hash(ss1.begin(), ss1.end()).GetLow64(); CDataStream ss2(SER_GETHASH, 0); ss2 << nKey << vchSourceGroupKey << (hash1 % ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP); - uint64_t hash2 = Hash(ss2.begin(), ss2.end()).Get64(); + uint64_t hash2 = Hash(ss2.begin(), ss2.end()).GetLow64(); return hash2 % ADDRMAN_NEW_BUCKET_COUNT; } diff --git a/src/uint256.h b/src/uint256.h index 7dbb3f83bba..c19d82ceb54 100644 --- a/src/uint256.h +++ b/src/uint256.h @@ -203,7 +203,7 @@ public: { // prefix operator int i = 0; - while (--pn[i] == -1 && i < WIDTH-1) + while (--pn[i] == (uint32_t)-1 && i < WIDTH-1) i++; return *this; } @@ -370,9 +370,10 @@ public: return sizeof(pn); } - uint64_t Get64(int n=0) const + uint64_t GetLow64() const { - return pn[2*n] | (uint64_t)pn[2*n+1] << 32; + assert(WIDTH >= 2); + return pn[0] | (uint64_t)pn[1] << 32; } // unsigned int GetSerializeSize(int nType=0, int nVersion=PROTOCOL_VERSION) const