mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-01-19 23:03:45 +01:00
refactor: Drop unsafe AsBytePtr function
Replace calls to AsBytePtr with direct calls to AsBytes or reinterpret_cast. AsBytePtr is just a wrapper around reinterpret_cast. It accepts any type of pointer as an argument and uses reinterpret_cast to cast the argument to a std::byte pointer. Despite taking any type of pointer as an argument, it is not useful to call AsBytePtr on most types of pointers, because byte representations of most types will be implmentation-specific. Also, because it is named similarly to the AsBytes function, AsBytePtr looks safer than it actually is. Both AsBytes and AsBytePtr call reinterpret_cast internally and may be unsafe to use with certain types, but AsBytes at least has some type checking and can only be called on Span objects, while AsBytePtr can be called on any pointer argument. Co-authored-by: Pieter Wuille <pieter@wuille.net>
This commit is contained in:
@@ -24,6 +24,12 @@
|
||||
namespace wallet {
|
||||
static constexpr int32_t WALLET_SCHEMA_VERSION = 0;
|
||||
|
||||
static Span<const std::byte> SpanFromBlob(sqlite3_stmt* stmt, int col)
|
||||
{
|
||||
return {reinterpret_cast<const std::byte*>(sqlite3_column_blob(stmt, col)),
|
||||
static_cast<size_t>(sqlite3_column_bytes(stmt, col))};
|
||||
}
|
||||
|
||||
static void ErrorLogCallback(void* arg, int code, const char* msg)
|
||||
{
|
||||
// From sqlite3_config() documentation for the SQLITE_CONFIG_LOG option:
|
||||
@@ -434,10 +440,8 @@ bool SQLiteBatch::ReadKey(DataStream&& key, DataStream& value)
|
||||
return false;
|
||||
}
|
||||
// Leftmost column in result is index 0
|
||||
const std::byte* data{AsBytePtr(sqlite3_column_blob(m_read_stmt, 0))};
|
||||
size_t data_size(sqlite3_column_bytes(m_read_stmt, 0));
|
||||
value.clear();
|
||||
value.write({data, data_size});
|
||||
value.write(SpanFromBlob(m_read_stmt, 0));
|
||||
|
||||
sqlite3_clear_bindings(m_read_stmt);
|
||||
sqlite3_reset(m_read_stmt);
|
||||
@@ -527,12 +531,8 @@ DatabaseCursor::Status SQLiteCursor::Next(DataStream& key, DataStream& value)
|
||||
value.clear();
|
||||
|
||||
// Leftmost column in result is index 0
|
||||
const std::byte* key_data{AsBytePtr(sqlite3_column_blob(m_cursor_stmt, 0))};
|
||||
size_t key_data_size(sqlite3_column_bytes(m_cursor_stmt, 0));
|
||||
key.write({key_data, key_data_size});
|
||||
const std::byte* value_data{AsBytePtr(sqlite3_column_blob(m_cursor_stmt, 1))};
|
||||
size_t value_data_size(sqlite3_column_bytes(m_cursor_stmt, 1));
|
||||
value.write({value_data, value_data_size});
|
||||
key.write(SpanFromBlob(m_cursor_stmt, 0));
|
||||
value.write(SpanFromBlob(m_cursor_stmt, 1));
|
||||
return Status::MORE;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user