Fix packer: store span_writer by reference, not copy

This commit is contained in:
Ian Gulliver
2026-04-10 23:23:58 +09:00
parent 7264611f99
commit f6d8847bcf
2 changed files with 6 additions and 5 deletions

View File

@@ -164,7 +164,7 @@ inline result<body_info> get_body_info(const uint8_t *p, int size) {
class packer {
private:
span_writer m_buf;
span_writer &m_buf;
template <typename T> void push_big_endian(T n) {
auto p = reinterpret_cast<std::uint8_t *>(&n) + (sizeof(T) - 1);
@@ -178,8 +178,7 @@ private:
}
public:
packer(uint8_t *data, size_t capacity) : m_buf(data, capacity) {}
packer(span_writer buf) : m_buf(buf) {}
packer(span_writer &buf) : m_buf(buf) {}
packer(const packer &) = delete;
packer &operator=(const packer &) = delete;
@@ -409,7 +408,8 @@ public:
requires requires(const T &v) { { T::ext_id } -> std::convertible_to<int8_t>; v.as_tuple(); }
pack_result pack(const T &v) {
uint8_t ext_buf[256];
packer inner(ext_buf, sizeof(ext_buf));
span_writer ext_writer(ext_buf, sizeof(ext_buf));
packer inner(ext_writer);
auto r = inner.pack(v.as_tuple());
if (!r) return r;
return pack_ext(T::ext_id, inner.get_payload());

View File

@@ -115,7 +115,8 @@ inline msgpack::result<size_t> encode_response_into(span_writer &out, uint32_t m
body_p.pack(msg.as_tuple());
auto inner_ext = out.subspan(envelope_hdr_len, ext16_header_len);
msgpack::packer(inner_ext).pack_ext16_header(T::ext_id, static_cast<uint16_t>(body.size()));
msgpack::packer inner_ext_p(inner_ext);
inner_ext_p.pack_ext16_header(T::ext_id, static_cast<uint16_t>(body.size()));
size_t bin_len = inner_ext.size() + body.size();
uint32_t checksum = halfsiphash::hash32({inner_ext.data(), bin_len}, hash_key);