Skip to content

Commit 32f1f02

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#21817: refactor: Replace &foo[0] with foo.data()
fac30ee refactor: Replace &foo[0] with foo.data() (MarcoFalke) faece47 refactor: Avoid &foo[0] on C-Style arrays (MarcoFalke) face961 refactor: Use only one temporary buffer in CreateObfuscateKey (MarcoFalke) fa05ddd refactor: Use CPubKey vector constructor where possible (MarcoFalke) fabb6df script: Replace address-of idiom with vector data() method (Guido Vranken) Pull request description: The main theme of this refactor is to replace `&foo[0]` with `foo.data()`. The first commit is taken from #21781 with the rationale: * In CSignatureCache::ComputeEntryECDSA, change the way a vector pointer is resolved to prevent invoking undefined behavior if the vector is empty. The other commits aim to remove all `&foo[0]`, where `foo` is any kind of byte representation. The rationale: * Sometimes alternative code without any raw data pointers is easier to read (refer to the respective commit message for details) * If the raw data pointer is needed, `foo.data()` should be preferred, as pointed out in the developer notes. This addresses the instances that have been missed in commit 592404f, and bitcoin/bitcoin#9804 ACKs for top commit: laanwj: Code review ACK fac30ee practicalswift: cr ACK fac30ee: patch looks correct promag: Code review ACK fac30ee. Tree-SHA512: e7e73146edbc78911a8e8c728b0a1c6b0ed9a88a008e650aa5dbffe72425bd42c76df70199a9cf7e02637448d7593e0eac52fd0f91f59240283e1390ee21bfa5
2 parents 1b9a523 + fac30ee commit 32f1f02

24 files changed

+57
-58
lines changed

build_msvc/testconsensus/testconsensus.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ int main()
4545
stream << vanillaSpendTx;
4646

4747
bitcoinconsensus_error err;
48-
auto op0Result = bitcoinconsensus_verify_script_with_amount(pubKeyScript.data(), pubKeyScript.size(), amount, (const unsigned char*)&stream[0], stream.size(), 0, bitcoinconsensus_SCRIPT_FLAGS_VERIFY_ALL, &err);
48+
auto op0Result = bitcoinconsensus_verify_script_with_amount(pubKeyScript.data(), pubKeyScript.size(), amount, stream.data(), stream.size(), 0, bitcoinconsensus_SCRIPT_FLAGS_VERIFY_ALL, &err);
4949
std::cout << "Op0 result: " << op0Result << ", error code " << err << std::endl;
5050

5151
getchar();

src/compressor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ bool DecompressScript(CScript& script, unsigned int nSize, const CompressedScrip
124124
unsigned char vch[33] = {};
125125
vch[0] = nSize - 2;
126126
memcpy(&vch[1], in.data(), 32);
127-
CPubKey pubkey(&vch[0], &vch[33]);
127+
CPubKey pubkey{vch};
128128
if (!pubkey.Decompress())
129129
return false;
130130
assert(pubkey.size() == 65);

src/dbwrapper.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,9 @@ const unsigned int CDBWrapper::OBFUSCATE_KEY_NUM_BYTES = 8;
220220
*/
221221
std::vector<unsigned char> CDBWrapper::CreateObfuscateKey() const
222222
{
223-
unsigned char buff[OBFUSCATE_KEY_NUM_BYTES];
224-
GetRandBytes(buff, OBFUSCATE_KEY_NUM_BYTES);
225-
return std::vector<unsigned char>(&buff[0], &buff[OBFUSCATE_KEY_NUM_BYTES]);
226-
223+
std::vector<uint8_t> ret(OBFUSCATE_KEY_NUM_BYTES);
224+
GetRandBytes(ret.data(), OBFUSCATE_KEY_NUM_BYTES);
225+
return ret;
227226
}
228227

229228
bool CDBWrapper::IsEmpty()

src/key.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ bool CKey::Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const
293293
bool CExtKey::Derive(CExtKey &out, unsigned int _nChild) const {
294294
out.nDepth = nDepth + 1;
295295
CKeyID id = key.GetPubKey().GetID();
296-
memcpy(&out.vchFingerprint[0], &id, 4);
296+
memcpy(out.vchFingerprint, &id, 4);
297297
out.nChild = _nChild;
298298
return key.Derive(out.key, out.chaincode, _nChild, chaincode);
299299
}
@@ -312,7 +312,7 @@ void CExtKey::SetSeed(const unsigned char *seed, unsigned int nSeedLen) {
312312
CExtPubKey CExtKey::Neuter() const {
313313
CExtPubKey ret;
314314
ret.nDepth = nDepth;
315-
memcpy(&ret.vchFingerprint[0], &vchFingerprint[0], 4);
315+
memcpy(ret.vchFingerprint, vchFingerprint, 4);
316316
ret.nChild = nChild;
317317
ret.pubkey = key.GetPubKey();
318318
ret.chaincode = chaincode;

src/key.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ struct CExtKey {
151151
friend bool operator==(const CExtKey& a, const CExtKey& b)
152152
{
153153
return a.nDepth == b.nDepth &&
154-
memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], sizeof(vchFingerprint)) == 0 &&
154+
memcmp(a.vchFingerprint, b.vchFingerprint, sizeof(vchFingerprint)) == 0 &&
155155
a.nChild == b.nChild &&
156156
a.chaincode == b.chaincode &&
157157
a.key == b.key;

src/pubkey.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ void CExtPubKey::Decode(const unsigned char code[BIP32_EXTKEY_SIZE]) {
293293
bool CExtPubKey::Derive(CExtPubKey &out, unsigned int _nChild) const {
294294
out.nDepth = nDepth + 1;
295295
CKeyID id = pubkey.GetID();
296-
memcpy(&out.vchFingerprint[0], &id, 4);
296+
memcpy(out.vchFingerprint, &id, 4);
297297
out.nChild = _nChild;
298298
return pubkey.Derive(out.pubkey, out.chaincode, _nChild, chaincode);
299299
}

src/pubkey.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class CPubKey
101101
}
102102

103103
//! Construct a public key from a byte vector.
104-
explicit CPubKey(const std::vector<unsigned char>& _vch)
104+
explicit CPubKey(Span<const uint8_t> _vch)
105105
{
106106
Set(_vch.begin(), _vch.end());
107107
}
@@ -247,7 +247,7 @@ struct CExtPubKey {
247247
friend bool operator==(const CExtPubKey &a, const CExtPubKey &b)
248248
{
249249
return a.nDepth == b.nDepth &&
250-
memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], sizeof(vchFingerprint)) == 0 &&
250+
memcmp(a.vchFingerprint, b.vchFingerprint, sizeof(vchFingerprint)) == 0 &&
251251
a.nChild == b.nChild &&
252252
a.chaincode == b.chaincode &&
253253
a.pubkey == b.pubkey;

src/qt/walletmodel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ WalletModel::SendCoinsReturn WalletModel::sendCoins(WalletModelTransaction &tran
248248

249249
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
250250
ssTx << *newTx;
251-
transaction_array.append((const char*)&(ssTx[0]), ssTx.size());
251+
transaction_array.append((const char*)ssTx.data(), ssTx.size());
252252
}
253253

254254
// Add addresses / update labels that we've sent to the address book,

src/random.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ std::vector<unsigned char> FastRandomContext::randbytes(size_t len)
628628
if (requires_seed) RandomSeed();
629629
std::vector<unsigned char> ret(len);
630630
if (len > 0) {
631-
rng.Keystream(&ret[0], len);
631+
rng.Keystream(ret.data(), len);
632632
}
633633
return ret;
634634
}

src/script/descriptor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,7 +1098,7 @@ std::unique_ptr<DescriptorImpl> InferScript(const CScript& script, ParseScriptCo
10981098
TxoutType txntype = Solver(script, data);
10991099

11001100
if (txntype == TxoutType::PUBKEY) {
1101-
CPubKey pubkey(data[0].begin(), data[0].end());
1101+
CPubKey pubkey(data[0]);
11021102
if (pubkey.IsValid()) {
11031103
return std::make_unique<PKDescriptor>(InferPubkey(pubkey, ctx, provider));
11041104
}
@@ -1122,7 +1122,7 @@ std::unique_ptr<DescriptorImpl> InferScript(const CScript& script, ParseScriptCo
11221122
if (txntype == TxoutType::MULTISIG) {
11231123
std::vector<std::unique_ptr<PubkeyProvider>> providers;
11241124
for (size_t i = 1; i + 1 < data.size(); ++i) {
1125-
CPubKey pubkey(data[i].begin(), data[i].end());
1125+
CPubKey pubkey(data[i]);
11261126
providers.push_back(InferPubkey(pubkey, ctx, provider));
11271127
}
11281128
return std::make_unique<MultisigDescriptor>((int)data[0][0], std::move(providers));

0 commit comments

Comments
 (0)