From dc2347cbc498a853fafd6c385ab67c01857f5a27 Mon Sep 17 00:00:00 2001 From: Marijn Schouten Date: Tue, 19 Nov 2024 14:40:05 +0100 Subject: [PATCH] Fix unsoundness of peek_ahead in iter.rs --- src/iter.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/iter.rs b/src/iter.rs index 4c5fa88..e74c317 100644 --- a/src/iter.rs +++ b/src/iter.rs @@ -43,12 +43,9 @@ impl<'a> Bytes<'a> { #[inline] pub fn peek_ahead(&self, n: usize) -> Option { - // 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 }