Skip to content

Commit 479afa0

Browse files
committed
Merge #9804: Fixes subscript 0 (&var[0]) where should use (var.data()) instead.
30ac768 Fix subscript[0] potential bugs in key.cpp (Jeremy Rubin) 4b1c0f2 Remove unnecessary branches in utilstrencodings string constructors. (Jeremy Rubin) e19db7b Fix subscript[0] in utilstrencodings.cpp (Jeremy Rubin) bc2e7fd Fix subscript[0] in streams.h (Jeremy Rubin) 4cac0d1 Fix subscript[0] in validation.cpp (Jeremy Rubin) ac658e5 Fix subscript[0] in torcontrol (Jeremy Rubin) b6856eb Fix subscript[0] in netaddress.cpp (Jeremy Rubin) 361d952 Fix subscript[0] in base58.cpp (Jeremy Rubin) 6896dbf Cleanup (safe, it was checked) subscript[0] in MurmurHash3 (and cleanup MurmurHash3 to be more clear). (Jeremy Rubin) 96f2119 Fix subscript[0] in compressor.cpp (Jeremy Rubin) 500710b Fix 2 subscript[0] bugs in pubkey.cpp, and eliminate one extra size check (Jeremy Rubin) e0451e3 Fix subscript[0] bug in net.cpp if GetGroup returns a 0-sized vector (Jeremy Rubin) Tree-SHA512: 5b9103652cf8c615bd8f4f32b3573d291d6b67c39e0308ce00100bc6625f346e8e016b4c999f4f34f5c37ae059490a83c3b513deb21f838af785227d06e02362
2 parents 2a09a38 + 30ac768 commit 479afa0

13 files changed

+51
-57
lines changed

src/base58.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend)
110110

111111
std::string EncodeBase58(const std::vector<unsigned char>& vch)
112112
{
113-
return EncodeBase58(&vch[0], &vch[0] + vch.size());
113+
return EncodeBase58(vch.data(), vch.data() + vch.size());
114114
}
115115

116116
bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet)
@@ -160,7 +160,7 @@ void CBase58Data::SetData(const std::vector<unsigned char>& vchVersionIn, const
160160
vchVersion = vchVersionIn;
161161
vchData.resize(nSize);
162162
if (!vchData.empty())
163-
memcpy(&vchData[0], pdata, nSize);
163+
memcpy(vchData.data(), pdata, nSize);
164164
}
165165

166166
void CBase58Data::SetData(const std::vector<unsigned char>& vchVersionIn, const unsigned char* pbegin, const unsigned char* pend)
@@ -180,8 +180,8 @@ bool CBase58Data::SetString(const char* psz, unsigned int nVersionBytes)
180180
vchVersion.assign(vchTemp.begin(), vchTemp.begin() + nVersionBytes);
181181
vchData.resize(vchTemp.size() - nVersionBytes);
182182
if (!vchData.empty())
183-
memcpy(&vchData[0], &vchTemp[nVersionBytes], vchData.size());
184-
memory_cleanse(&vchTemp[0], vchTemp.size());
183+
memcpy(vchData.data(), vchTemp.data() + nVersionBytes, vchData.size());
184+
memory_cleanse(vchTemp.data(), vchTemp.size());
185185
return true;
186186
}
187187

@@ -262,7 +262,7 @@ CTxDestination CBitcoinAddress::Get() const
262262
if (!IsValid())
263263
return CNoDestination();
264264
uint160 id;
265-
memcpy(&id, &vchData[0], 20);
265+
memcpy(&id, vchData.data(), 20);
266266
if (vchVersion == Params().Base58Prefix(CChainParams::PUBKEY_ADDRESS))
267267
return CKeyID(id);
268268
else if (vchVersion == Params().Base58Prefix(CChainParams::SCRIPT_ADDRESS))
@@ -276,7 +276,7 @@ bool CBitcoinAddress::GetKeyID(CKeyID& keyID) const
276276
if (!IsValid() || vchVersion != Params().Base58Prefix(CChainParams::PUBKEY_ADDRESS))
277277
return false;
278278
uint160 id;
279-
memcpy(&id, &vchData[0], 20);
279+
memcpy(&id, vchData.data(), 20);
280280
keyID = CKeyID(id);
281281
return true;
282282
}

src/base58.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ template<typename K, int Size, CChainParams::Base58Type Type> class CBitcoinExtK
148148
K ret;
149149
if (vchData.size() == Size) {
150150
// If base58 encoded data does not hold an ext key, return a !IsValid() key
151-
ret.Decode(&vchData[0]);
151+
ret.Decode(vchData.data());
152152
}
153153
return ret;
154154
}

src/compressor.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,30 +93,30 @@ bool CScriptCompressor::Decompress(unsigned int nSize, const std::vector<unsigne
9393
script[0] = OP_DUP;
9494
script[1] = OP_HASH160;
9595
script[2] = 20;
96-
memcpy(&script[3], &in[0], 20);
96+
memcpy(&script[3], in.data(), 20);
9797
script[23] = OP_EQUALVERIFY;
9898
script[24] = OP_CHECKSIG;
9999
return true;
100100
case 0x01:
101101
script.resize(23);
102102
script[0] = OP_HASH160;
103103
script[1] = 20;
104-
memcpy(&script[2], &in[0], 20);
104+
memcpy(&script[2], in.data(), 20);
105105
script[22] = OP_EQUAL;
106106
return true;
107107
case 0x02:
108108
case 0x03:
109109
script.resize(35);
110110
script[0] = 33;
111111
script[1] = nSize;
112-
memcpy(&script[2], &in[0], 32);
112+
memcpy(&script[2], in.data(), 32);
113113
script[34] = OP_CHECKSIG;
114114
return true;
115115
case 0x04:
116116
case 0x05:
117117
unsigned char vch[33] = {};
118118
vch[0] = nSize - 2;
119-
memcpy(&vch[1], &in[0], 32);
119+
memcpy(&vch[1], in.data(), 32);
120120
CPubKey pubkey(&vch[0], &vch[33]);
121121
if (!pubkey.Decompress())
122122
return false;

src/hash.cpp

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,34 @@ unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector<unsigned char
1717
{
1818
// The following is MurmurHash3 (x86_32), see http://code.google.com/p/smhasher/source/browse/trunk/MurmurHash3.cpp
1919
uint32_t h1 = nHashSeed;
20-
if (vDataToHash.size() > 0)
21-
{
22-
const uint32_t c1 = 0xcc9e2d51;
23-
const uint32_t c2 = 0x1b873593;
20+
const uint32_t c1 = 0xcc9e2d51;
21+
const uint32_t c2 = 0x1b873593;
2422

25-
const int nblocks = vDataToHash.size() / 4;
23+
const int nblocks = vDataToHash.size() / 4;
2624

27-
//----------
28-
// body
29-
const uint8_t* blocks = &vDataToHash[0] + nblocks * 4;
25+
//----------
26+
// body
27+
const uint8_t* blocks = vDataToHash.data();
3028

31-
for (int i = -nblocks; i; i++) {
32-
uint32_t k1 = ReadLE32(blocks + i*4);
29+
for (int i = 0; i < nblocks; ++i) {
30+
uint32_t k1 = ReadLE32(blocks + i*4);
3331

34-
k1 *= c1;
35-
k1 = ROTL32(k1, 15);
36-
k1 *= c2;
32+
k1 *= c1;
33+
k1 = ROTL32(k1, 15);
34+
k1 *= c2;
3735

38-
h1 ^= k1;
39-
h1 = ROTL32(h1, 13);
40-
h1 = h1 * 5 + 0xe6546b64;
41-
}
36+
h1 ^= k1;
37+
h1 = ROTL32(h1, 13);
38+
h1 = h1 * 5 + 0xe6546b64;
39+
}
4240

43-
//----------
44-
// tail
45-
const uint8_t* tail = (const uint8_t*)(&vDataToHash[0] + nblocks * 4);
41+
//----------
42+
// tail
43+
const uint8_t* tail = vDataToHash.data() + nblocks * 4;
4644

47-
uint32_t k1 = 0;
45+
uint32_t k1 = 0;
4846

49-
switch (vDataToHash.size() & 3) {
47+
switch (vDataToHash.size() & 3) {
5048
case 3:
5149
k1 ^= tail[2] << 16;
5250
case 2:
@@ -57,7 +55,6 @@ unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector<unsigned char
5755
k1 = ROTL32(k1, 15);
5856
k1 *= c2;
5957
h1 ^= k1;
60-
}
6158
}
6259

6360
//----------

src/key.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ CPrivKey CKey::GetPrivKey() const {
138138
size_t privkeylen;
139139
privkey.resize(279);
140140
privkeylen = 279;
141-
ret = ec_privkey_export_der(secp256k1_context_sign, (unsigned char*)&privkey[0], &privkeylen, begin(), fCompressed ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED);
141+
ret = ec_privkey_export_der(secp256k1_context_sign, (unsigned char*) privkey.data(), &privkeylen, begin(), fCompressed ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED);
142142
assert(ret);
143143
privkey.resize(privkeylen);
144144
return privkey;
@@ -167,7 +167,7 @@ bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig, uint32_
167167
secp256k1_ecdsa_signature sig;
168168
int ret = secp256k1_ecdsa_sign(secp256k1_context_sign, &sig, hash.begin(), begin(), secp256k1_nonce_function_rfc6979, test_case ? extra_entropy : NULL);
169169
assert(ret);
170-
secp256k1_ecdsa_signature_serialize_der(secp256k1_context_sign, (unsigned char*)&vchSig[0], &nSigLen, &sig);
170+
secp256k1_ecdsa_signature_serialize_der(secp256k1_context_sign, (unsigned char*)vchSig.data(), &nSigLen, &sig);
171171
vchSig.resize(nSigLen);
172172
return true;
173173
}
@@ -202,7 +202,7 @@ bool CKey::SignCompact(const uint256 &hash, std::vector<unsigned char>& vchSig)
202202
}
203203

204204
bool CKey::Load(CPrivKey &privkey, CPubKey &vchPubKey, bool fSkipCheck=false) {
205-
if (!ec_privkey_import_der(secp256k1_context_sign, (unsigned char*)begin(), &privkey[0], privkey.size()))
205+
if (!ec_privkey_import_der(secp256k1_context_sign, (unsigned char*)begin(), privkey.data(), privkey.size()))
206206
return false;
207207
fCompressed = vchPubKey.IsCompressed();
208208
fValid = true;
@@ -245,8 +245,8 @@ void CExtKey::SetMaster(const unsigned char *seed, unsigned int nSeedLen) {
245245
static const unsigned char hashkey[] = {'B','i','t','c','o','i','n',' ','s','e','e','d'};
246246
std::vector<unsigned char, secure_allocator<unsigned char>> vout(64);
247247
CHMAC_SHA512(hashkey, sizeof(hashkey)).Write(seed, nSeedLen).Finalize(vout.data());
248-
key.Set(&vout[0], &vout[32], true);
249-
memcpy(chaincode.begin(), &vout[32], 32);
248+
key.Set(vout.data(), vout.data() + 32, true);
249+
memcpy(chaincode.begin(), vout.data() + 32, 32);
250250
nDepth = 0;
251251
nChild = 0;
252252
memset(vchFingerprint, 0, sizeof(vchFingerprint));

src/net.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2876,5 +2876,5 @@ uint64_t CConnman::CalculateKeyedNetGroup(const CAddress& ad) const
28762876
{
28772877
std::vector<unsigned char> vchNetGroup(ad.GetGroup());
28782878

2879-
return GetDeterministicRandomizer(RANDOMIZER_ID_NETGROUP).Write(&vchNetGroup[0], vchNetGroup.size()).Finalize();
2879+
return GetDeterministicRandomizer(RANDOMIZER_ID_NETGROUP).Write(vchNetGroup.data(), vchNetGroup.size()).Finalize();
28802880
}

src/netaddress.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ std::vector<unsigned char> CService::GetKey() const
573573
{
574574
std::vector<unsigned char> vKey;
575575
vKey.resize(18);
576-
memcpy(&vKey[0], ip, 16);
576+
memcpy(vKey.data(), ip, 16);
577577
vKey[16] = port / 0x100;
578578
vKey[17] = port & 0x0FF;
579579
return vKey;

src/pubkey.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,7 @@ bool CPubKey::Verify(const uint256 &hash, const std::vector<unsigned char>& vchS
172172
if (!secp256k1_ec_pubkey_parse(secp256k1_context_verify, &pubkey, &(*this)[0], size())) {
173173
return false;
174174
}
175-
if (vchSig.size() == 0) {
176-
return false;
177-
}
178-
if (!ecdsa_signature_parse_der_lax(secp256k1_context_verify, &sig, &vchSig[0], vchSig.size())) {
175+
if (!ecdsa_signature_parse_der_lax(secp256k1_context_verify, &sig, vchSig.data(), vchSig.size())) {
179176
return false;
180177
}
181178
/* libsecp256k1's ECDSA verification requires lower-S signatures, which have
@@ -274,7 +271,7 @@ bool CExtPubKey::Derive(CExtPubKey &out, unsigned int _nChild) const {
274271

275272
/* static */ bool CPubKey::CheckLowS(const std::vector<unsigned char>& vchSig) {
276273
secp256k1_ecdsa_signature sig;
277-
if (!ecdsa_signature_parse_der_lax(secp256k1_context_verify, &sig, &vchSig[0], vchSig.size())) {
274+
if (!ecdsa_signature_parse_der_lax(secp256k1_context_verify, &sig, vchSig.data(), vchSig.size())) {
278275
return false;
279276
}
280277
return (!secp256k1_ecdsa_signature_normalize(secp256k1_context_verify, NULL, &sig));

src/serialize.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -450,15 +450,15 @@ class LimitedString
450450
}
451451
string.resize(size);
452452
if (size != 0)
453-
s.read((char*)&string[0], size);
453+
s.read((char*)string.data(), size);
454454
}
455455

456456
template<typename Stream>
457457
void Serialize(Stream& s) const
458458
{
459459
WriteCompactSize(s, string.size());
460460
if (!string.empty())
461-
s.write((char*)&string[0], string.size());
461+
s.write((char*)string.data(), string.size());
462462
}
463463
};
464464

@@ -556,7 +556,7 @@ void Serialize(Stream& os, const std::basic_string<C>& str)
556556
{
557557
WriteCompactSize(os, str.size());
558558
if (!str.empty())
559-
os.write((char*)&str[0], str.size() * sizeof(str[0]));
559+
os.write((char*)str.data(), str.size() * sizeof(C));
560560
}
561561

562562
template<typename Stream, typename C>
@@ -565,7 +565,7 @@ void Unserialize(Stream& is, std::basic_string<C>& str)
565565
unsigned int nSize = ReadCompactSize(is);
566566
str.resize(nSize);
567567
if (nSize != 0)
568-
is.read((char*)&str[0], nSize * sizeof(str[0]));
568+
is.read((char*)str.data(), nSize * sizeof(C));
569569
}
570570

571571

@@ -578,7 +578,7 @@ void Serialize_impl(Stream& os, const prevector<N, T>& v, const unsigned char&)
578578
{
579579
WriteCompactSize(os, v.size());
580580
if (!v.empty())
581-
os.write((char*)&v[0], v.size() * sizeof(T));
581+
os.write((char*)v.data(), v.size() * sizeof(T));
582582
}
583583

584584
template<typename Stream, unsigned int N, typename T, typename V>
@@ -646,7 +646,7 @@ void Serialize_impl(Stream& os, const std::vector<T, A>& v, const unsigned char&
646646
{
647647
WriteCompactSize(os, v.size());
648648
if (!v.empty())
649-
os.write((char*)&v[0], v.size() * sizeof(T));
649+
os.write((char*)v.data(), v.size() * sizeof(T));
650650
}
651651

652652
template<typename Stream, typename T, typename A, typename V>

src/streams.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ class CDataStream
389389
{
390390
// Special case: stream << stream concatenates like stream += stream
391391
if (!vch.empty())
392-
s.write((char*)&vch[0], vch.size() * sizeof(vch[0]));
392+
s.write((char*)vch.data(), vch.size() * sizeof(value_type));
393393
}
394394

395395
template<typename T>

0 commit comments

Comments
 (0)