Skip to content

Commit

Permalink
fix IoSlice cursor bug
Browse files Browse the repository at this point in the history
  • Loading branch information
camshaft committed Feb 8, 2024
1 parent d6d0ca6 commit 476e4da
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
18 changes: 14 additions & 4 deletions quic/s2n-quic-core/src/buffer/reader/storage/buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,13 @@ where
match chunk_len.cmp(&dest.remaining_capacity()) {
// if there's more chunks left, then copy this one out and keep going
Ordering::Less if self.buf.remaining() > chunk_len => {
dest.put_slice(self.buf.chunk());
self.buf.advance(chunk_len);
if Dest::SPECIALIZES_BYTES {
let chunk = self.buf.copy_to_bytes(chunk_len);
dest.put_bytes(chunk);
} else {
dest.put_slice(self.buf.chunk());
self.buf.advance(chunk_len);
}
continue;
}
Ordering::Less | Ordering::Equal => {
Expand Down Expand Up @@ -120,8 +125,13 @@ where

ensure!(len > 0, Ok(()));

dest.put_slice(&chunk[..len]);
self.buf.advance(len);
if Dest::SPECIALIZES_BYTES {
let chunk = self.buf.copy_to_bytes(len);
dest.put_bytes(chunk);
} else {
dest.put_slice(&chunk[..len]);
self.buf.advance(len);
}
}
}
}
Expand Down
30 changes: 21 additions & 9 deletions quic/s2n-quic-core/src/buffer/reader/storage/io_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ where
#[inline]
pub fn new(buf: &'a [T]) -> Self {
let mut len = 0;
let mut first_non_empty = 0;
let mut first_non_empty = usize::MAX;
let mut last_non_empty = 0;

// find the total length and the first non-empty slice
Expand Down Expand Up @@ -56,6 +56,7 @@ where
buf,
};
slice.advance_buf_once();
slice.invariants();
slice
}

Expand Down Expand Up @@ -87,15 +88,8 @@ where

#[inline]
fn set_len(&mut self, len: usize) {
if cfg!(debug_assertions) {
// make sure the computed len matches the actual remaining len
let mut computed = self.head.len();
for buf in self.buf.iter() {
computed += buf.len();
}
assert_eq!(len, computed);
}
self.len = len;
self.invariants();
}

#[inline]
Expand Down Expand Up @@ -136,6 +130,24 @@ where
}
}
}

#[inline(always)]
fn invariants(&self) {
#[cfg(debug_assertions)]
{
// make sure the computed len matches the actual remaining len
let mut computed = self.head.len();
for buf in self.buf.iter() {
computed += buf.len();
}
assert_eq!(self.len, computed);

if self.head.is_empty() {
assert!(self.buf.is_empty());
assert_eq!(self.len, 0);
}
}
}
}

impl<'a, T> bytes::Buf for IoSlice<'a, T>
Expand Down

0 comments on commit 476e4da

Please sign in to comment.