mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-11 22:50:59 +01:00
Merge bitcoin/bitcoin#27978: refactor: Drop unsafe AsBytePtr function
7c853619eerefactor: Drop unsafe AsBytePtr function (Ryan Ofsky) Pull request description: Replace calls to `AsBytePtr` with 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 platform specific or undefined. 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. The change was motivated by discussion on #27973 and #27927 and is compatible with those PRs ACKs for top commit: jonatack: re-ACK7c853619eesipa: utACK7c853619eeachow101: ACK7c853619eeTree-SHA512: 200d858b1d4d579f081a7f9a14d488a99713b4918b4564ac3dd5c18578d927dbd6426e62e02f49f04a3fa73ca02ff7109c495cb0b92bec43c27d9b74e2f95757
This commit is contained in:
@@ -473,10 +473,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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -486,10 +486,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));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user