Skip to content

Commit fccec82

Browse files
authored
fix: avoid dereferencing past-the-end vector iterators in serialize.hpp (#22261)
## Summary Two `write()` overloads in `serialize.hpp` used `&*buf.end()` to obtain a raw pointer into the newly-resized region. Under `_GLIBCXX_DEBUG` (enabled in asan-fast and debug presets since #22218), dereferencing `end()` is a debug assertion abort — even though the subtracted pointer was in-bounds. Replaced with `buf.data() + buf.size()` which yields the same pointer without touching any iterator. ## Failure `ChonkTests.Basic` in the asan-fast build aborted with: ``` Error: attempt to dereference a past-the-end iterator. ``` ## Fix - `serialize.hpp:166`: `&*buf.end() - sizeof(value)` → `buf.data() + buf.size() - sizeof(value)` - `serialize.hpp:251`: `&*buf.end() - N` → `buf.data() + buf.size() - N` ## Test plan - [x] `ChonkTests.Basic` passes with debug-fast preset (`_GLIBCXX_DEBUG` enabled) ClaudeBox log: https://claudebox.work/s/4aef0cbe07a366e4?run=1
1 parent ed6069d commit fccec82

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

barretenberg/cpp/src/barretenberg/common/serialize.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ void read(std::vector<uint8_t> const& buf, std::integral auto& value)
163163
void write(std::vector<uint8_t>& buf, const std::integral auto& value)
164164
{
165165
buf.resize(buf.size() + sizeof(value));
166-
uint8_t* ptr = &*buf.end() - sizeof(value);
166+
uint8_t* ptr = buf.data() + buf.size() - sizeof(value);
167167
write(ptr, value);
168168
}
169169

@@ -248,7 +248,7 @@ inline void write(std::ostream& os, std::vector<uint8_t> const& value)
248248
template <size_t N> inline void write(std::vector<uint8_t>& buf, std::array<uint8_t, N> const& value)
249249
{
250250
buf.resize(buf.size() + N);
251-
auto* ptr = &*buf.end() - N;
251+
auto* ptr = buf.data() + buf.size() - N;
252252
write(ptr, value);
253253
}
254254

0 commit comments

Comments
 (0)