Skip to content

Commit fa9108f

Browse files
author
MarcoFalke
committed
refactor: Use reinterpret_cast where appropriate
Also, wrap reinterpret_cast into a CharCast to ensure it is only called on byte pointers.
1 parent 3333f95 commit fa9108f

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

Diff for: src/dbwrapper.h

+10-8
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ struct DBParams {
5454
DBOptions options{};
5555
};
5656

57+
inline auto CharCast(const std::byte* data) { return reinterpret_cast<const char*>(data); }
58+
5759
class dbwrapper_error : public std::runtime_error
5860
{
5961
public:
@@ -113,12 +115,12 @@ class CDBBatch
113115
{
114116
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
115117
ssKey << key;
116-
leveldb::Slice slKey((const char*)ssKey.data(), ssKey.size());
118+
leveldb::Slice slKey(CharCast(ssKey.data()), ssKey.size());
117119

118120
ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
119121
ssValue << value;
120122
ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent));
121-
leveldb::Slice slValue((const char*)ssValue.data(), ssValue.size());
123+
leveldb::Slice slValue(CharCast(ssValue.data()), ssValue.size());
122124

123125
batch.Put(slKey, slValue);
124126
// LevelDB serializes writes as:
@@ -138,7 +140,7 @@ class CDBBatch
138140
{
139141
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
140142
ssKey << key;
141-
leveldb::Slice slKey((const char*)ssKey.data(), ssKey.size());
143+
leveldb::Slice slKey(CharCast(ssKey.data()), ssKey.size());
142144

143145
batch.Delete(slKey);
144146
// LevelDB serializes erases as:
@@ -177,7 +179,7 @@ class CDBIterator
177179
DataStream ssKey{};
178180
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
179181
ssKey << key;
180-
leveldb::Slice slKey((const char*)ssKey.data(), ssKey.size());
182+
leveldb::Slice slKey(CharCast(ssKey.data()), ssKey.size());
181183
piter->Seek(slKey);
182184
}
183185

@@ -265,7 +267,7 @@ class CDBWrapper
265267
DataStream ssKey{};
266268
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
267269
ssKey << key;
268-
leveldb::Slice slKey((const char*)ssKey.data(), ssKey.size());
270+
leveldb::Slice slKey(CharCast(ssKey.data()), ssKey.size());
269271

270272
std::string strValue;
271273
leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
@@ -307,7 +309,7 @@ class CDBWrapper
307309
DataStream ssKey{};
308310
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
309311
ssKey << key;
310-
leveldb::Slice slKey((const char*)ssKey.data(), ssKey.size());
312+
leveldb::Slice slKey(CharCast(ssKey.data()), ssKey.size());
311313

312314
std::string strValue;
313315
leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
@@ -351,8 +353,8 @@ class CDBWrapper
351353
ssKey2.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
352354
ssKey1 << key_begin;
353355
ssKey2 << key_end;
354-
leveldb::Slice slKey1((const char*)ssKey1.data(), ssKey1.size());
355-
leveldb::Slice slKey2((const char*)ssKey2.data(), ssKey2.size());
356+
leveldb::Slice slKey1(CharCast(ssKey1.data()), ssKey1.size());
357+
leveldb::Slice slKey2(CharCast(ssKey2.data()), ssKey2.size());
356358
uint64_t size = 0;
357359
leveldb::Range range(slKey1, slKey2);
358360
pdb->GetApproximateSizes(&range, 1, &size);

Diff for: src/span.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
#ifndef BITCOIN_SPAN_H
66
#define BITCOIN_SPAN_H
77

8-
#include <type_traits>
9-
#include <cstddef>
108
#include <algorithm>
11-
#include <assert.h>
9+
#include <cassert>
10+
#include <cstddef>
11+
#include <type_traits>
1212

1313
#ifdef DEBUG
1414
#define CONSTEXPR_IF_NOT_DEBUG
@@ -267,9 +267,9 @@ Span<std::byte> MakeWritableByteSpan(V&& v) noexcept
267267
}
268268

269269
// Helper functions to safely cast to unsigned char pointers.
270-
inline unsigned char* UCharCast(char* c) { return (unsigned char*)c; }
270+
inline unsigned char* UCharCast(char* c) { return reinterpret_cast<unsigned char*>(c); }
271271
inline unsigned char* UCharCast(unsigned char* c) { return c; }
272-
inline unsigned char* UCharCast(std::byte* c) { return (unsigned char*)c; }
272+
inline unsigned char* UCharCast(std::byte* c) { return reinterpret_cast<unsigned char*>(c); }
273273
inline const unsigned char* UCharCast(const char* c) { return reinterpret_cast<const unsigned char*>(c); }
274274
inline const unsigned char* UCharCast(const unsigned char* c) { return c; }
275275
inline const unsigned char* UCharCast(const std::byte* c) { return reinterpret_cast<const unsigned char*>(c); }

0 commit comments

Comments
 (0)