]> git.lizzy.rs Git - rust.git/commitdiff
Rollup merge of #32855 - troplin:take-bufread-fix, r=alexcrichton
authorSteve Klabnik <steve@steveklabnik.com>
Thu, 14 Apr 2016 18:49:09 +0000 (14:49 -0400)
committerSteve Klabnik <steve@steveklabnik.com>
Thu, 14 Apr 2016 18:49:09 +0000 (14:49 -0400)
Don't read past limit for in BufRead instance of Take

Similar to `Read::read`, `BufRead::fill_buf` impl of `Take` should not call `inner.fill_buf` if the limit is already reached.

src/libstd/io/mod.rs

index 6dd7273c17fe7b614cebcc67cc4cac7c68e0468f..d914d143e7011ae3683c30054ecd817a91a83992 100644 (file)
@@ -1505,6 +1505,11 @@ fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<T: BufRead> BufRead for Take<T> {
     fn fill_buf(&mut self) -> Result<&[u8]> {
+        // Don't call into inner reader at all at EOF because it may still block
+        if self.limit == 0 {
+            return Ok(&[]);
+        }
+
         let buf = self.inner.fill_buf()?;
         let cap = cmp::min(buf.len() as u64, self.limit) as usize;
         Ok(&buf[..cap])
@@ -1860,9 +1865,16 @@ fn read(&mut self, _: &mut [u8]) -> io::Result<usize> {
                 Err(io::Error::new(io::ErrorKind::Other, ""))
             }
         }
+        impl BufRead for R {
+            fn fill_buf(&mut self) -> io::Result<&[u8]> {
+                Err(io::Error::new(io::ErrorKind::Other, ""))
+            }
+            fn consume(&mut self, _amt: usize) { }
+        }
 
         let mut buf = [0; 1];
         assert_eq!(0, R.take(0).read(&mut buf).unwrap());
+        assert_eq!(b"", R.take(0).fill_buf().unwrap());
     }
 
     fn cmp_bufread<Br1: BufRead, Br2: BufRead>(mut br1: Br1, mut br2: Br2, exp: &[u8]) {