Skip to content

Commit

Permalink
Don't have important data in unused capacity when calling reserve (#563)
Browse files Browse the repository at this point in the history
  • Loading branch information
Darksonn authored Aug 12, 2022
1 parent b724914 commit d1b5d4c
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/bytes_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,15 @@ impl BytesMut {
new_cap = cmp::max(double, new_cap);

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

// Update the info
Expand Down

0 comments on commit d1b5d4c

Please sign in to comment.