Skip to content

Commit db8f4d6

Browse files
committed
Rollup merge of rust-lang#32855 - troplin:take-bufread-fix, r=alexcrichton
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.
2 parents 634911d + a3329f5 commit db8f4d6

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/libstd/io/mod.rs

+12
Original file line numberDiff line numberDiff line change
@@ -1505,6 +1505,11 @@ impl<T: Read> Read for Take<T> {
15051505
#[stable(feature = "rust1", since = "1.0.0")]
15061506
impl<T: BufRead> BufRead for Take<T> {
15071507
fn fill_buf(&mut self) -> Result<&[u8]> {
1508+
// Don't call into inner reader at all at EOF because it may still block
1509+
if self.limit == 0 {
1510+
return Ok(&[]);
1511+
}
1512+
15081513
let buf = self.inner.fill_buf()?;
15091514
let cap = cmp::min(buf.len() as u64, self.limit) as usize;
15101515
Ok(&buf[..cap])
@@ -1860,9 +1865,16 @@ mod tests {
18601865
Err(io::Error::new(io::ErrorKind::Other, ""))
18611866
}
18621867
}
1868+
impl BufRead for R {
1869+
fn fill_buf(&mut self) -> io::Result<&[u8]> {
1870+
Err(io::Error::new(io::ErrorKind::Other, ""))
1871+
}
1872+
fn consume(&mut self, _amt: usize) { }
1873+
}
18631874

18641875
let mut buf = [0; 1];
18651876
assert_eq!(0, R.take(0).read(&mut buf).unwrap());
1877+
assert_eq!(b"", R.take(0).fill_buf().unwrap());
18661878
}
18671879

18681880
fn cmp_bufread<Br1: BufRead, Br2: BufRead>(mut br1: Br1, mut br2: Br2, exp: &[u8]) {

0 commit comments

Comments
 (0)