Skip to content

Commit 133c727

Browse files
committed
Merge #8321: [trivial] Switched constants to sizeof()
fbc6070 [trivial] Switched constants to sizeof() (Thomas Snider)
2 parents 2266b43 + fbc6070 commit 133c727

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

src/key.h

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include <vector>
1616

1717

18-
/**
18+
/**
1919
* secp256k1:
2020
* const unsigned int PRIVATE_KEY_SIZE = 279;
2121
* const unsigned int PUBLIC_KEY_SIZE = 65;
@@ -45,6 +45,8 @@ class CKey
4545
//! The actual byte data
4646
unsigned char vch[32];
4747

48+
static_assert(sizeof(vch) == 32, "vch must be 32 bytes in length to not break serialization");
49+
4850
//! Check whether the 32-byte array pointed to be vch is valid keydata.
4951
bool static Check(const unsigned char* vch);
5052

@@ -70,20 +72,19 @@ class CKey
7072

7173
friend bool operator==(const CKey& a, const CKey& b)
7274
{
73-
return a.fCompressed == b.fCompressed && a.size() == b.size() &&
74-
memcmp(&a.vch[0], &b.vch[0], a.size()) == 0;
75+
return a.fCompressed == b.fCompressed &&
76+
a.size() == b.size() &&
77+
memcmp(&a.vch[0], &b.vch[0], a.size()) == 0;
7578
}
7679

7780
//! Initialize using begin and end iterators to byte data.
7881
template <typename T>
7982
void Set(const T pbegin, const T pend, bool fCompressedIn)
8083
{
81-
if (pend - pbegin != 32) {
84+
if (pend - pbegin != sizeof(vch)) {
8285
fValid = false;
83-
return;
84-
}
85-
if (Check(&pbegin[0])) {
86-
memcpy(vch, (unsigned char*)&pbegin[0], 32);
86+
} else if (Check(&pbegin[0])) {
87+
memcpy(vch, (unsigned char*)&pbegin[0], sizeof(vch));
8788
fValid = true;
8889
fCompressed = fCompressedIn;
8990
} else {
@@ -92,7 +93,7 @@ class CKey
9293
}
9394

9495
//! Simple read-only vector-like interface.
95-
unsigned int size() const { return (fValid ? 32 : 0); }
96+
unsigned int size() const { return (fValid ? sizeof(vch) : 0); }
9697
const unsigned char* begin() const { return vch; }
9798
const unsigned char* end() const { return vch + size(); }
9899

@@ -110,7 +111,7 @@ class CKey
110111

111112
/**
112113
* Convert the private key to a CPrivKey (serialized OpenSSL private key data).
113-
* This is expensive.
114+
* This is expensive.
114115
*/
115116
CPrivKey GetPrivKey() const;
116117

@@ -157,8 +158,11 @@ struct CExtKey {
157158

158159
friend bool operator==(const CExtKey& a, const CExtKey& b)
159160
{
160-
return a.nDepth == b.nDepth && memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], 4) == 0 && a.nChild == b.nChild &&
161-
a.chaincode == b.chaincode && a.key == b.key;
161+
return a.nDepth == b.nDepth &&
162+
memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], sizeof(vchFingerprint)) == 0 &&
163+
a.nChild == b.nChild &&
164+
a.chaincode == b.chaincode &&
165+
a.key == b.key;
162166
}
163167

164168
void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const;

src/pubkey.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include <stdexcept>
1414
#include <vector>
1515

16-
/**
16+
/**
1717
* secp256k1:
1818
* const unsigned int PRIVATE_KEY_SIZE = 279;
1919
* const unsigned int PUBLIC_KEY_SIZE = 65;
@@ -156,7 +156,7 @@ class CPubKey
156156

157157
/*
158158
* Check syntactic correctness.
159-
*
159+
*
160160
* Note that this is consensus critical as CheckSig() calls it!
161161
*/
162162
bool IsValid() const
@@ -203,8 +203,11 @@ struct CExtPubKey {
203203

204204
friend bool operator==(const CExtPubKey &a, const CExtPubKey &b)
205205
{
206-
return a.nDepth == b.nDepth && memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], 4) == 0 && a.nChild == b.nChild &&
207-
a.chaincode == b.chaincode && a.pubkey == b.pubkey;
206+
return a.nDepth == b.nDepth &&
207+
memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], sizeof(vchFingerprint)) == 0 &&
208+
a.nChild == b.nChild &&
209+
a.chaincode == b.chaincode &&
210+
a.pubkey == b.pubkey;
208211
}
209212

210213
void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const;

0 commit comments

Comments
 (0)