Fix infinite loop in BufReader::read_until.

This commit is contained in:
Q.P.Liu 2014-02-08 17:53:27 -08:00
parent e9c539a488
commit 71c88e7f47
1 changed files with 9 additions and 1 deletions

View File

@ -312,7 +312,11 @@ impl<'a> Seek for BufReader<'a> {
impl<'a> Buffer for BufReader<'a> {
fn fill<'a>(&'a mut self) -> IoResult<&'a [u8]> {
Ok(self.buf.slice_from(self.pos))
if self.pos < self.buf.len() {
Ok(self.buf.slice_from(self.pos))
} else {
Err(io::standard_error(io::EndOfFile))
}
}
fn consume(&mut self, amt: uint) { self.pos += amt; }
}
@ -449,6 +453,10 @@ mod test {
assert_eq!(reader.read(buf), Ok(3));
assert_eq!(buf.slice(0, 3), [5, 6, 7]);
assert!(reader.read(buf).is_err());
let mut reader = BufReader::new(in_buf);
assert_eq!(reader.read_until(3).unwrap(), ~[0, 1, 2, 3]);
assert_eq!(reader.read_until(3).unwrap(), ~[4, 5, 6, 7]);
assert!(reader.read(buf).is_err());
}
#[test]