diff --git a/src/dbwrapper.h b/src/dbwrapper.h index 35782edca6b..4ae2106211a 100644 --- a/src/dbwrapper.h +++ b/src/dbwrapper.h @@ -54,6 +54,8 @@ struct DBParams { DBOptions options{}; }; +inline auto CharCast(const std::byte* data) { return reinterpret_cast(data); } + class dbwrapper_error : public std::runtime_error { public: @@ -113,12 +115,12 @@ public: { ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); ssKey << key; - leveldb::Slice slKey((const char*)ssKey.data(), ssKey.size()); + leveldb::Slice slKey(CharCast(ssKey.data()), ssKey.size()); ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE); ssValue << value; ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent)); - leveldb::Slice slValue((const char*)ssValue.data(), ssValue.size()); + leveldb::Slice slValue(CharCast(ssValue.data()), ssValue.size()); batch.Put(slKey, slValue); // LevelDB serializes writes as: @@ -138,7 +140,7 @@ public: { ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); ssKey << key; - leveldb::Slice slKey((const char*)ssKey.data(), ssKey.size()); + leveldb::Slice slKey(CharCast(ssKey.data()), ssKey.size()); batch.Delete(slKey); // LevelDB serializes erases as: @@ -177,7 +179,7 @@ public: DataStream ssKey{}; ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); ssKey << key; - leveldb::Slice slKey((const char*)ssKey.data(), ssKey.size()); + leveldb::Slice slKey(CharCast(ssKey.data()), ssKey.size()); piter->Seek(slKey); } @@ -265,7 +267,7 @@ public: DataStream ssKey{}; ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); ssKey << key; - leveldb::Slice slKey((const char*)ssKey.data(), ssKey.size()); + leveldb::Slice slKey(CharCast(ssKey.data()), ssKey.size()); std::string strValue; leveldb::Status status = pdb->Get(readoptions, slKey, &strValue); @@ -307,7 +309,7 @@ public: DataStream ssKey{}; ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); ssKey << key; - leveldb::Slice slKey((const char*)ssKey.data(), ssKey.size()); + leveldb::Slice slKey(CharCast(ssKey.data()), ssKey.size()); std::string strValue; leveldb::Status status = pdb->Get(readoptions, slKey, &strValue); @@ -351,8 +353,8 @@ public: ssKey2.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); ssKey1 << key_begin; ssKey2 << key_end; - leveldb::Slice slKey1((const char*)ssKey1.data(), ssKey1.size()); - leveldb::Slice slKey2((const char*)ssKey2.data(), ssKey2.size()); + leveldb::Slice slKey1(CharCast(ssKey1.data()), ssKey1.size()); + leveldb::Slice slKey2(CharCast(ssKey2.data()), ssKey2.size()); uint64_t size = 0; leveldb::Range range(slKey1, slKey2); pdb->GetApproximateSizes(&range, 1, &size); diff --git a/src/span.h b/src/span.h index 792a0a9c075..7209d21a585 100644 --- a/src/span.h +++ b/src/span.h @@ -5,10 +5,10 @@ #ifndef BITCOIN_SPAN_H #define BITCOIN_SPAN_H -#include -#include #include -#include +#include +#include +#include #ifdef DEBUG #define CONSTEXPR_IF_NOT_DEBUG @@ -267,9 +267,9 @@ Span MakeWritableByteSpan(V&& v) noexcept } // Helper functions to safely cast to unsigned char pointers. -inline unsigned char* UCharCast(char* c) { return (unsigned char*)c; } +inline unsigned char* UCharCast(char* c) { return reinterpret_cast(c); } inline unsigned char* UCharCast(unsigned char* c) { return c; } -inline unsigned char* UCharCast(std::byte* c) { return (unsigned char*)c; } +inline unsigned char* UCharCast(std::byte* c) { return reinterpret_cast(c); } inline const unsigned char* UCharCast(const char* c) { return reinterpret_cast(c); } inline const unsigned char* UCharCast(const unsigned char* c) { return c; } inline const unsigned char* UCharCast(const std::byte* c) { return reinterpret_cast(c); }