diff options
author | MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> | 2023-07-22 16:07:29 +0200 |
---|---|---|
committer | MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> | 2023-07-24 15:32:35 +0200 |
commit | fa9108f85afdc926fd6a8b96cc2acff7ca25d7a8 (patch) | |
tree | 669d4ff3edc8e863d397bf2e9d20313be4a794cb /src | |
parent | 3333f950d49f13662842650ae76599a0dff052eb (diff) |
refactor: Use reinterpret_cast where appropriate
Also, wrap reinterpret_cast into a CharCast to ensure it is only called
on byte pointers.
Diffstat (limited to 'src')
-rw-r--r-- | src/dbwrapper.h | 18 | ||||
-rw-r--r-- | src/span.h | 10 |
2 files changed, 15 insertions, 13 deletions
diff --git a/src/dbwrapper.h b/src/dbwrapper.h index 35782edca6..4ae2106211 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<const char*>(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 792a0a9c07..7209d21a58 100644 --- a/src/span.h +++ b/src/span.h @@ -5,10 +5,10 @@ #ifndef BITCOIN_SPAN_H #define BITCOIN_SPAN_H -#include <type_traits> -#include <cstddef> #include <algorithm> -#include <assert.h> +#include <cassert> +#include <cstddef> +#include <type_traits> #ifdef DEBUG #define CONSTEXPR_IF_NOT_DEBUG @@ -267,9 +267,9 @@ Span<std::byte> 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<unsigned char*>(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<unsigned char*>(c); } inline const unsigned char* UCharCast(const char* c) { return reinterpret_cast<const unsigned char*>(c); } inline const unsigned char* UCharCast(const unsigned char* c) { return c; } inline const unsigned char* UCharCast(const std::byte* c) { return reinterpret_cast<const unsigned char*>(c); } |