Properly handle EOF in varint-rs (#106)

This commit is contained in:
Pierre Krieger
2018-01-23 19:23:37 +01:00
committed by GitHub
parent bd17f2ea96
commit 71dae91d15

View File

@ -378,8 +378,12 @@ impl<T: Default + DecoderHelper> Decoder for VarintDecoder<T> {
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
loop {
if src.is_empty() && self.state.is_some() {
if src.is_empty() {
if self.state.is_some() {
break Err(io::Error::from(io::ErrorKind::UnexpectedEof));
} else {
break Ok(None);
}
} else {
// We know that the length is not 0, so this cannot fail.
let first_byte = src.split_to(1)[0];
@ -633,4 +637,12 @@ mod tests {
assert_eq!(result.unwrap_err().kind(), io::ErrorKind::UnexpectedEof);
}
#[test]
fn no_panic_after_eof() {
FramedRead::new(&[1, 1][..], VarintDecoder::<usize>::new())
.collect()
.wait()
.unwrap();
}
}