fix: remove assertion in Cursor when reading past end
What changed, and why it matters
This commit fixes a panic in a custom Cursor-like reader used by the rust-bitcoin project. Previously, reading past the end of a buffer could trigger an assertion failure (a crash). The fix makes the custom Cursor behave like Rust's standard library Cursor: it returns an empty slice when positioned past the end and avoids panicking when consuming bytes. This is a robustness fix that prevents denial-of-service-style crashes in code that reads untrusted or malformed data.
Treat as a low-to-moderate security/robustness fix. Review callers of this Cursor to confirm they handle empty fill_buf results and EOF correctly. No immediate emergency response is indicated, but the fix should be included in the next release and backported if the affected code is used in production parsing paths.
Security signals we found
Removal of assertion that could panic on untrusted input
Fix for out-of-bounds/past-end slice indexing in fill_buf()
Behavior alignment with std::io::Cursor, which is a known safe reference implementation
Potential denial-of-service vector via panic in parsing code
Evidence from the diff
The patch modifies the BufRead implementation for Cursor
Changed components
io/src/lib.rsCursor<T> BufRead implementationfill_buf()consume()Inspect captured patch +3 / −3
diff --git a/io/src/lib.rs b/io/src/lib.rs
index 0f4c27db..6e287150 100644
--- a/io/src/lib.rs
+++ b/io/src/lib.rs
@@ -299,13 +299,13 @@ impl<T: AsRef<[u8]>> BufRead for Cursor<T> {
#[inline]
fn fill_buf(&mut self) -> Result<&[u8]> {
let inner: &[u8] = self.inner.as_ref();
- Ok(&inner[self.pos as usize..])
+ let pos = self.pos.min(inner.len() as u64) as usize;
+ Ok(&inner[pos..])
}
#[inline]
fn consume(&mut self, amount: usize) {
- assert!(amount <= self.inner.as_ref().len());
- self.pos += amount as u64;
+ self.pos = self.pos.saturating_add(amount as u64);
}
}
Why this scored 37/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.