aboutsummaryrefslogtreecommitdiff
path: root/src/serialize.h
diff options
context:
space:
mode:
authorRyan Ofsky <ryan@ofsky.org>2023-06-26 12:12:20 -0400
committerRyan Ofsky <ryan@ofsky.org>2023-06-28 15:14:45 -0400
commit7c853619ee9ea17a79f1234b6c7871a45ceadcb9 (patch)
treef50720eefda193caf23b487d9e92da418553007f /src/serialize.h
parent7ee41217b3b3fe4d8b7eb4fd1d4577b9b33d466d (diff)
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>
Diffstat (limited to 'src/serialize.h')
-rw-r--r--src/serialize.h8
1 files changed, 4 insertions, 4 deletions
diff --git a/src/serialize.h b/src/serialize.h
index 7bc7b10779..c46e5a5640 100644
--- a/src/serialize.h
+++ b/src/serialize.h
@@ -472,10 +472,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));
}
}
@@ -485,10 +485,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));
}
}