about summary refs log tree commit diff
path: root/library/std/src/io/buffered/bufreader.rs
diff options
context:
space:
mode:
authorbinarycat <binarycat@envs.net>2024-09-06 16:28:22 -0400
committerbinarycat <binarycat@envs.net>2024-09-06 16:28:22 -0400
commitdfdbf6343ae1ea97b504b36ec1933e181ec7198e (patch)
tree732f527cd05ffd122c73d307643ce17cc79094cc /library/std/src/io/buffered/bufreader.rs
parent17b322fa69eed7216dccc9f097eb68237cf62234 (diff)
properly handle EOF in BufReader::peek
previously this would cause an infinite loop due to it being
unable to read `n` bytes.
Diffstat (limited to 'library/std/src/io/buffered/bufreader.rs')
-rw-r--r--library/std/src/io/buffered/bufreader.rs12
1 files changed, 10 insertions, 2 deletions
diff --git a/library/std/src/io/buffered/bufreader.rs b/library/std/src/io/buffered/bufreader.rs
index cf226bd28d0..b69d7edb5a7 100644
--- a/library/std/src/io/buffered/bufreader.rs
+++ b/library/std/src/io/buffered/bufreader.rs
@@ -99,7 +99,10 @@ impl<R: Read> BufReader<R> {
 impl<R: Read + ?Sized> BufReader<R> {
     /// Attempt to look ahead `n` bytes.
     ///
-    /// `n` must be less than `capacity`.
+    /// `n` must be less than or equal to `capacity`.
+    ///
+    /// the returned slice may be less than `n` bytes long if
+    /// end of file is reached.
     ///
     /// ## Examples
     ///
@@ -117,6 +120,7 @@ impl<R: Read + ?Sized> BufReader<R> {
     /// let mut s = String::new();
     /// rdr.read_to_string(&mut s).unwrap();
     /// assert_eq!(&s, "hello");
+    /// assert_eq!(rdr.peek(1).unwrap().len(), 0);
     /// ```
     #[unstable(feature = "bufreader_peek", issue = "128405")]
     pub fn peek(&mut self, n: usize) -> io::Result<&[u8]> {
@@ -125,7 +129,11 @@ impl<R: Read + ?Sized> BufReader<R> {
             if self.buf.pos() > 0 {
                 self.buf.backshift();
             }
-            self.buf.read_more(&mut self.inner)?;
+            let new = self.buf.read_more(&mut self.inner)?;
+            if new == 0 {
+                // end of file, no more bytes to read
+                return Ok(&self.buf.buffer()[..]);
+            }
             debug_assert_eq!(self.buf.pos(), 0);
         }
         Ok(&self.buf.buffer()[..n])