Skip to content

Commit d1b5d4c

Browse files
authored
Don't have important data in unused capacity when calling reserve (#563)
1 parent b724914 commit d1b5d4c

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/bytes_mut.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,15 @@ impl BytesMut {
705705
new_cap = cmp::max(double, new_cap);
706706

707707
// No space - allocate more
708+
//
709+
// The length field of `Shared::vec` is not used by the `BytesMut`;
710+
// instead we use the `len` field in the `BytesMut` itself. However,
711+
// when calling `reserve`, it doesn't guarantee that data stored in
712+
// the unused capacity of the vector is copied over to the new
713+
// allocation, so we need to ensure that we don't have any data we
714+
// care about in the unused capacity before calling `reserve`.
715+
debug_assert!(off + len <= v.capacity());
716+
v.set_len(off + len);
708717
v.reserve(new_cap - v.len());
709718

710719
// Update the info

0 commit comments

Comments
 (0)