Skip to content

Commit

Permalink
Fix unsoundness of peek_ahead in iter.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
hkBst committed Dec 8, 2024
1 parent 380f130 commit dc2347c
Showing 1 changed file with 2 additions and 5 deletions.
7 changes: 2 additions & 5 deletions src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,9 @@ impl<'a> Bytes<'a> {

#[inline]
pub fn peek_ahead(&self, n: usize) -> Option<u8> {
// SAFETY: obtain a potentially OOB pointer that is later compared against the `self.end`
// pointer.
let ptr = self.cursor.wrapping_add(n);
if ptr < self.end {
if n < self.len() {
// SAFETY: bounds checked pointer dereference is safe
Some(unsafe { *ptr })
Some(unsafe { *self.cursor.add(n) })
} else {
None
}
Expand Down

0 comments on commit dc2347c

Please sign in to comment.