Fix infinite loop in MemReader::read_until.

This commit is contained in:
Q.P.Liu 2014-02-07 21:50:49 -08:00
parent a2290db797
commit e9c539a488

View File

@ -177,7 +177,11 @@ impl Seek for MemReader {
impl Buffer for MemReader {
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; }
}
@ -421,6 +425,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 = MemReader::new(~[0, 1, 2, 3, 4, 5, 6, 7]);
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]