diff options
author | 2022-08-12 12:22:43 +0200 | |
---|---|---|
committer | 2022-08-12 12:22:43 +0200 | |
commit | d1b5d4ceb1645d0843729479a04be6c119a04369 (patch) | |
tree | f9c857c131fa204ac4a8ed4a316ab29d669d19a7 /src | |
parent | b7249149a926f0d5f9f89897853eb7dc7506d27b (diff) | |
download | bytes-d1b5d4ceb1645d0843729479a04be6c119a04369.tar.gz bytes-d1b5d4ceb1645d0843729479a04be6c119a04369.tar.zst bytes-d1b5d4ceb1645d0843729479a04be6c119a04369.zip |
Don't have important data in unused capacity when calling reserve (#563)
Diffstat (limited to 'src')
-rw-r--r-- | src/bytes_mut.rs | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/src/bytes_mut.rs b/src/bytes_mut.rs index 2282bdc..a292ca7 100644 --- a/src/bytes_mut.rs +++ b/src/bytes_mut.rs @@ -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 |