Skip to content

Commit fada14b

Browse files
author
MarcoFalke
committedNov 23, 2020
Treat CDataStream bytes as uint8_t
Also, rename CSerializeData to SerializeData
1 parent fa8bdb0 commit fada14b

File tree

7 files changed

+25
-41
lines changed

7 files changed

+25
-41
lines changed
 

‎src/dbwrapper.h

+10-10
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ class CDBBatch
7474
{
7575
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
7676
ssKey << key;
77-
leveldb::Slice slKey(ssKey.data(), ssKey.size());
77+
leveldb::Slice slKey((const char*)ssKey.data(), ssKey.size());
7878

7979
ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
8080
ssValue << value;
8181
ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent));
82-
leveldb::Slice slValue(ssValue.data(), ssValue.size());
82+
leveldb::Slice slValue((const char*)ssValue.data(), ssValue.size());
8383

8484
batch.Put(slKey, slValue);
8585
// LevelDB serializes writes as:
@@ -99,7 +99,7 @@ class CDBBatch
9999
{
100100
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
101101
ssKey << key;
102-
leveldb::Slice slKey(ssKey.data(), ssKey.size());
102+
leveldb::Slice slKey((const char*)ssKey.data(), ssKey.size());
103103

104104
batch.Delete(slKey);
105105
// LevelDB serializes erases as:
@@ -138,7 +138,7 @@ class CDBIterator
138138
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
139139
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
140140
ssKey << key;
141-
leveldb::Slice slKey(ssKey.data(), ssKey.size());
141+
leveldb::Slice slKey((const char*)ssKey.data(), ssKey.size());
142142
piter->Seek(slKey);
143143
}
144144

@@ -233,7 +233,7 @@ class CDBWrapper
233233
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
234234
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
235235
ssKey << key;
236-
leveldb::Slice slKey(ssKey.data(), ssKey.size());
236+
leveldb::Slice slKey((const char*)ssKey.data(), ssKey.size());
237237

238238
std::string strValue;
239239
leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
@@ -267,7 +267,7 @@ class CDBWrapper
267267
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
268268
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
269269
ssKey << key;
270-
leveldb::Slice slKey(ssKey.data(), ssKey.size());
270+
leveldb::Slice slKey((const char*)ssKey.data(), ssKey.size());
271271

272272
std::string strValue;
273273
leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
@@ -311,8 +311,8 @@ class CDBWrapper
311311
ssKey2.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
312312
ssKey1 << key_begin;
313313
ssKey2 << key_end;
314-
leveldb::Slice slKey1(ssKey1.data(), ssKey1.size());
315-
leveldb::Slice slKey2(ssKey2.data(), ssKey2.size());
314+
leveldb::Slice slKey1((const char*)ssKey1.data(), ssKey1.size());
315+
leveldb::Slice slKey2((const char*)ssKey2.data(), ssKey2.size());
316316
uint64_t size = 0;
317317
leveldb::Range range(slKey1, slKey2);
318318
pdb->GetApproximateSizes(&range, 1, &size);
@@ -330,8 +330,8 @@ class CDBWrapper
330330
ssKey2.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
331331
ssKey1 << key_begin;
332332
ssKey2 << key_end;
333-
leveldb::Slice slKey1(ssKey1.data(), ssKey1.size());
334-
leveldb::Slice slKey2(ssKey2.data(), ssKey2.size());
333+
leveldb::Slice slKey1((const char*)ssKey1.data(), ssKey1.size());
334+
leveldb::Slice slKey2((const char*)ssKey2.data(), ssKey2.size());
335335
pdb->CompactRange(&slKey1, &slKey2);
336336
}
337337
};

‎src/qt/walletmodel.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ WalletModel::SendCoinsReturn WalletModel::sendCoins(WalletModelTransaction &tran
244244

245245
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
246246
ssTx << *newTx;
247-
transaction_array.append(&(ssTx[0]), ssTx.size());
247+
transaction_array.append((const char*)&(ssTx[0]), ssTx.size());
248248
}
249249

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

‎src/streams.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ class VectorReader
203203
class CDataStream
204204
{
205205
protected:
206-
typedef CSerializeData vector_type;
206+
using vector_type = SerializeData;
207207
vector_type vch;
208208
unsigned int nReadPos;
209209

@@ -266,8 +266,8 @@ class CDataStream
266266
const_reference operator[](size_type pos) const { return vch[pos + nReadPos]; }
267267
reference operator[](size_type pos) { return vch[pos + nReadPos]; }
268268
void clear() { vch.clear(); nReadPos = 0; }
269-
iterator insert(iterator it, const char x=char()) { return vch.insert(it, x); }
270-
void insert(iterator it, size_type n, const char x) { vch.insert(it, n, x); }
269+
iterator insert(iterator it, const uint8_t x) { return vch.insert(it, x); }
270+
void insert(iterator it, size_type n, const uint8_t x) { vch.insert(it, n, x); }
271271
value_type* data() { return vch.data() + nReadPos; }
272272
const value_type* data() const { return vch.data() + nReadPos; }
273273

‎src/support/allocators/zeroafterfree.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ struct zero_after_free_allocator : public std::allocator<T> {
4242
}
4343
};
4444

45-
// Byte-vector that clears its contents before deletion.
46-
typedef std::vector<char, zero_after_free_allocator<char> > CSerializeData;
45+
/** Byte-vector that clears its contents before deletion. */
46+
using SerializeData = std::vector<uint8_t, zero_after_free_allocator<uint8_t>>;
4747

4848
#endif // BITCOIN_SUPPORT_ALLOCATORS_ZEROAFTERFREE_H

‎src/test/bloom_tests.cpp

+4-20
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,7 @@ BOOST_AUTO_TEST_CASE(bloom_create_insert_serialize)
4242
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
4343
stream << filter;
4444

45-
std::vector<unsigned char> vch = ParseHex("03614e9b050000000000000001");
46-
std::vector<char> expected(vch.size());
47-
48-
for (unsigned int i = 0; i < vch.size(); i++)
49-
expected[i] = (char)vch[i];
45+
std::vector<uint8_t> expected = ParseHex("03614e9b050000000000000001");
5046

5147
BOOST_CHECK_EQUAL_COLLECTIONS(stream.begin(), stream.end(), expected.begin(), expected.end());
5248

@@ -72,11 +68,7 @@ BOOST_AUTO_TEST_CASE(bloom_create_insert_serialize_with_tweak)
7268
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
7369
stream << filter;
7470

75-
std::vector<unsigned char> vch = ParseHex("03ce4299050000000100008001");
76-
std::vector<char> expected(vch.size());
77-
78-
for (unsigned int i = 0; i < vch.size(); i++)
79-
expected[i] = (char)vch[i];
71+
std::vector<uint8_t> expected = ParseHex("03ce4299050000000100008001");
8072

8173
BOOST_CHECK_EQUAL_COLLECTIONS(stream.begin(), stream.end(), expected.begin(), expected.end());
8274
}
@@ -96,11 +88,7 @@ BOOST_AUTO_TEST_CASE(bloom_create_insert_key)
9688
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
9789
stream << filter;
9890

99-
std::vector<unsigned char> vch = ParseHex("038fc16b080000000000000001");
100-
std::vector<char> expected(vch.size());
101-
102-
for (unsigned int i = 0; i < vch.size(); i++)
103-
expected[i] = (char)vch[i];
91+
std::vector<unsigned char> expected = ParseHex("038fc16b080000000000000001");
10492

10593
BOOST_CHECK_EQUAL_COLLECTIONS(stream.begin(), stream.end(), expected.begin(), expected.end());
10694
}
@@ -352,11 +340,7 @@ BOOST_AUTO_TEST_CASE(merkle_block_3_and_serialize)
352340
CDataStream merkleStream(SER_NETWORK, PROTOCOL_VERSION);
353341
merkleStream << merkleBlock;
354342

355-
std::vector<unsigned char> vch = ParseHex("0100000079cda856b143d9db2c1caff01d1aecc8630d30625d10e8b4b8b0000000000000b50cc069d6a3e33e3ff84a5c41d9d3febe7c770fdcc96b2c3ff60abe184f196367291b4d4c86041b8fa45d630100000001b50cc069d6a3e33e3ff84a5c41d9d3febe7c770fdcc96b2c3ff60abe184f19630101");
356-
std::vector<char> expected(vch.size());
357-
358-
for (unsigned int i = 0; i < vch.size(); i++)
359-
expected[i] = (char)vch[i];
343+
std::vector<uint8_t> expected = ParseHex("0100000079cda856b143d9db2c1caff01d1aecc8630d30625d10e8b4b8b0000000000000b50cc069d6a3e33e3ff84a5c41d9d3febe7c770fdcc96b2c3ff60abe184f196367291b4d4c86041b8fa45d630100000001b50cc069d6a3e33e3ff84a5c41d9d3febe7c770fdcc96b2c3ff60abe184f19630101");
360344

361345
BOOST_CHECK_EQUAL_COLLECTIONS(expected.begin(), expected.end(), merkleStream.begin(), merkleStream.end());
362346
}

‎src/test/serialize_tests.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ BOOST_AUTO_TEST_CASE(insert_delete)
320320

321321
ss.insert(ss.end(), c);
322322
BOOST_CHECK_EQUAL(ss.size(), 6U);
323-
BOOST_CHECK_EQUAL(ss[4], (char)0xff);
323+
BOOST_CHECK_EQUAL(ss[4], 0xff);
324324
BOOST_CHECK_EQUAL(ss[5], c);
325325

326326
ss.insert(ss.begin()+2, c);
@@ -334,14 +334,14 @@ BOOST_AUTO_TEST_CASE(insert_delete)
334334

335335
ss.erase(ss.begin()+ss.size()-1);
336336
BOOST_CHECK_EQUAL(ss.size(), 5U);
337-
BOOST_CHECK_EQUAL(ss[4], (char)0xff);
337+
BOOST_CHECK_EQUAL(ss[4], 0xff);
338338

339339
ss.erase(ss.begin()+1);
340340
BOOST_CHECK_EQUAL(ss.size(), 4U);
341341
BOOST_CHECK_EQUAL(ss[0], 0);
342342
BOOST_CHECK_EQUAL(ss[1], 1);
343343
BOOST_CHECK_EQUAL(ss[2], 2);
344-
BOOST_CHECK_EQUAL(ss[3], (char)0xff);
344+
BOOST_CHECK_EQUAL(ss[3], 0xff);
345345
}
346346

347347
BOOST_AUTO_TEST_CASE(class_methods)

‎src/wallet/bdb.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -491,9 +491,9 @@ bool BerkeleyDatabase::Rewrite(const char* pszSkip)
491491
break;
492492
}
493493
if (pszSkip &&
494-
strncmp(ssKey.data(), pszSkip, std::min(ssKey.size(), strlen(pszSkip))) == 0)
494+
strncmp((const char*)ssKey.data(), pszSkip, std::min(ssKey.size(), strlen(pszSkip))) == 0)
495495
continue;
496-
if (strncmp(ssKey.data(), "\x07version", 8) == 0) {
496+
if (strncmp((const char*)ssKey.data(), "\x07version", 8) == 0) {
497497
// Update version:
498498
ssValue.clear();
499499
ssValue << CLIENT_VERSION;

0 commit comments

Comments
 (0)