mirror of
https://github.com/dashpay/dash.git
synced 2024-12-24 19:42:46 +01:00
partial bitcoin#27978: Drop unsafe AsBytePtr function
excludes - some changes to `src/wallet/bdb.cpp`
This commit is contained in:
parent
67b0621edf
commit
3910a7547f
@ -17,12 +17,12 @@ static void EllSwiftCreate(benchmark::Bench& bench)
|
||||
uint256 entropy = GetRandHash();
|
||||
|
||||
bench.batch(1).unit("pubkey").run([&] {
|
||||
auto ret = key.EllSwiftCreate(AsBytes(Span{entropy}));
|
||||
auto ret = key.EllSwiftCreate(MakeByteSpan(entropy));
|
||||
/* Use the first 32 bytes of the ellswift encoded public key as next private key. */
|
||||
key.Set(UCharCast(ret.data()), UCharCast(ret.data()) + 32, true);
|
||||
assert(key.IsValid());
|
||||
/* Use the last 32 bytes of the ellswift encoded public key as next entropy. */
|
||||
std::copy(ret.begin() + 32, ret.begin() + 64, AsBytePtr(entropy.data()));
|
||||
std::copy(ret.begin() + 32, ret.begin() + 64, MakeWritableByteSpan(entropy).begin());
|
||||
});
|
||||
|
||||
ECC_Stop();
|
||||
|
@ -656,10 +656,10 @@ struct CustomUintFormatter
|
||||
if (v < 0 || v > MAX) throw std::ios_base::failure("CustomUintFormatter value out of range");
|
||||
if (BigEndian) {
|
||||
uint64_t raw = htobe64(v);
|
||||
s.write({AsBytePtr(&raw) + 8 - Bytes, Bytes});
|
||||
s.write(AsBytes(Span{&raw, 1}).last(Bytes));
|
||||
} else {
|
||||
uint64_t raw = htole64(v);
|
||||
s.write({AsBytePtr(&raw), Bytes});
|
||||
s.write(AsBytes(Span{&raw, 1}).first(Bytes));
|
||||
}
|
||||
}
|
||||
|
||||
@ -669,10 +669,10 @@ struct CustomUintFormatter
|
||||
static_assert(std::numeric_limits<U>::max() >= MAX && std::numeric_limits<U>::min() <= 0, "Assigned type too small");
|
||||
uint64_t raw = 0;
|
||||
if (BigEndian) {
|
||||
s.read({AsBytePtr(&raw) + 8 - Bytes, Bytes});
|
||||
s.read(AsWritableBytes(Span{&raw, 1}).last(Bytes));
|
||||
v = static_cast<I>(be64toh(raw));
|
||||
} else {
|
||||
s.read({AsBytePtr(&raw), Bytes});
|
||||
s.read(AsWritableBytes(Span{&raw, 1}).first(Bytes));
|
||||
v = static_cast<I>(le64toh(raw));
|
||||
}
|
||||
}
|
||||
|
@ -243,21 +243,16 @@ T& SpanPopBack(Span<T>& span)
|
||||
return back;
|
||||
}
|
||||
|
||||
//! Convert a data pointer to a std::byte data pointer.
|
||||
//! Where possible, please use the safer AsBytes helpers.
|
||||
inline const std::byte* AsBytePtr(const void* data) { return reinterpret_cast<const std::byte*>(data); }
|
||||
inline std::byte* AsBytePtr(void* data) { return reinterpret_cast<std::byte*>(data); }
|
||||
|
||||
// From C++20 as_bytes and as_writeable_bytes
|
||||
template <typename T>
|
||||
Span<const std::byte> AsBytes(Span<T> s) noexcept
|
||||
{
|
||||
return {AsBytePtr(s.data()), s.size_bytes()};
|
||||
return {reinterpret_cast<const std::byte*>(s.data()), s.size_bytes()};
|
||||
}
|
||||
template <typename T>
|
||||
Span<std::byte> AsWritableBytes(Span<T> s) noexcept
|
||||
{
|
||||
return {AsBytePtr(s.data()), s.size_bytes()};
|
||||
return {reinterpret_cast<std::byte*>(s.data()), s.size_bytes()};
|
||||
}
|
||||
|
||||
template <typename V>
|
||||
|
@ -17,6 +17,10 @@
|
||||
#endif
|
||||
|
||||
namespace {
|
||||
Span<const std::byte> SpanFromDbt(const BerkeleyBatch::SafeDbt& dbt)
|
||||
{
|
||||
return {reinterpret_cast<const std::byte*>(dbt.get_data()), dbt.get_size()};
|
||||
}
|
||||
|
||||
//! Make sure database has a unique fileid within the environment. If it
|
||||
//! doesn't, throw an error. BDB caches do not work properly when more than one
|
||||
@ -681,10 +685,10 @@ bool BerkeleyBatch::ReadAtCursor(CDataStream& ssKey, CDataStream& ssValue, bool&
|
||||
// Convert to streams
|
||||
ssKey.SetType(SER_DISK);
|
||||
ssKey.clear();
|
||||
ssKey.write({AsBytePtr(datKey.get_data()), datKey.get_size()});
|
||||
ssKey.write(SpanFromDbt(datKey));
|
||||
ssValue.SetType(SER_DISK);
|
||||
ssValue.clear();
|
||||
ssValue.write({AsBytePtr(datValue.get_data()), datValue.get_size()});
|
||||
ssValue.write(SpanFromDbt(datValue));
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -757,7 +761,7 @@ bool BerkeleyBatch::ReadKey(CDataStream&& key, CDataStream& value)
|
||||
int ret = pdb->get(activeTxn, datKey, datValue, 0);
|
||||
if (ret == 0 && datValue.get_data() != nullptr) {
|
||||
value.clear();
|
||||
value.write({AsBytePtr(datValue.get_data()), datValue.get_size()});
|
||||
value.write(SpanFromDbt(datValue));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
@ -168,6 +168,7 @@ public:
|
||||
/** RAII class that provides access to a Berkeley database */
|
||||
class BerkeleyBatch : public DatabaseBatch
|
||||
{
|
||||
public:
|
||||
/** RAII class that automatically cleanses its data on destruction */
|
||||
class SafeDbt final
|
||||
{
|
||||
|
@ -25,6 +25,12 @@ static constexpr int32_t WALLET_SCHEMA_VERSION = 0;
|
||||
static Mutex g_sqlite_mutex;
|
||||
static int g_sqlite_count GUARDED_BY(g_sqlite_mutex) = 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:
|
||||
@ -404,10 +410,8 @@ bool SQLiteBatch::ReadKey(CDataStream&& key, CDataStream& 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);
|
||||
@ -500,12 +504,8 @@ bool SQLiteBatch::ReadAtCursor(CDataStream& key, CDataStream& value, bool& compl
|
||||
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 true;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user