Merge #8321: [trivial] Switched constants to sizeof()

fbc6070 [trivial] Switched constants to sizeof() (Thomas Snider)
This commit is contained in:
Wladimir J. van der Laan 2016-07-28 13:30:15 +02:00
commit 133c727cc4
No known key found for this signature in database
GPG Key ID: 74810B012346C9A6
2 changed files with 23 additions and 16 deletions

View File

@ -45,6 +45,8 @@ private:
//! The actual byte data //! The actual byte data
unsigned char vch[32]; unsigned char vch[32];
static_assert(sizeof(vch) == 32, "vch must be 32 bytes in length to not break serialization");
//! Check whether the 32-byte array pointed to be vch is valid keydata. //! Check whether the 32-byte array pointed to be vch is valid keydata.
bool static Check(const unsigned char* vch); bool static Check(const unsigned char* vch);
@ -70,7 +72,8 @@ public:
friend bool operator==(const CKey& a, const CKey& b) friend bool operator==(const CKey& a, const CKey& b)
{ {
return a.fCompressed == b.fCompressed && a.size() == b.size() && return a.fCompressed == b.fCompressed &&
a.size() == b.size() &&
memcmp(&a.vch[0], &b.vch[0], a.size()) == 0; memcmp(&a.vch[0], &b.vch[0], a.size()) == 0;
} }
@ -78,12 +81,10 @@ public:
template <typename T> template <typename T>
void Set(const T pbegin, const T pend, bool fCompressedIn) void Set(const T pbegin, const T pend, bool fCompressedIn)
{ {
if (pend - pbegin != 32) { if (pend - pbegin != sizeof(vch)) {
fValid = false; fValid = false;
return; } else if (Check(&pbegin[0])) {
} memcpy(vch, (unsigned char*)&pbegin[0], sizeof(vch));
if (Check(&pbegin[0])) {
memcpy(vch, (unsigned char*)&pbegin[0], 32);
fValid = true; fValid = true;
fCompressed = fCompressedIn; fCompressed = fCompressedIn;
} else { } else {
@ -92,7 +93,7 @@ public:
} }
//! Simple read-only vector-like interface. //! Simple read-only vector-like interface.
unsigned int size() const { return (fValid ? 32 : 0); } unsigned int size() const { return (fValid ? sizeof(vch) : 0); }
const unsigned char* begin() const { return vch; } const unsigned char* begin() const { return vch; }
const unsigned char* end() const { return vch + size(); } const unsigned char* end() const { return vch + size(); }
@ -157,8 +158,11 @@ struct CExtKey {
friend bool operator==(const CExtKey& a, const CExtKey& b) friend bool operator==(const CExtKey& a, const CExtKey& b)
{ {
return a.nDepth == b.nDepth && memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], 4) == 0 && a.nChild == b.nChild && return a.nDepth == b.nDepth &&
a.chaincode == b.chaincode && a.key == b.key; memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], sizeof(vchFingerprint)) == 0 &&
a.nChild == b.nChild &&
a.chaincode == b.chaincode &&
a.key == b.key;
} }
void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const; void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const;

View File

@ -203,8 +203,11 @@ struct CExtPubKey {
friend bool operator==(const CExtPubKey &a, const CExtPubKey &b) friend bool operator==(const CExtPubKey &a, const CExtPubKey &b)
{ {
return a.nDepth == b.nDepth && memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], 4) == 0 && a.nChild == b.nChild && return a.nDepth == b.nDepth &&
a.chaincode == b.chaincode && a.pubkey == b.pubkey; memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], sizeof(vchFingerprint)) == 0 &&
a.nChild == b.nChild &&
a.chaincode == b.chaincode &&
a.pubkey == b.pubkey;
} }
void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const; void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const;