mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-29 17:51:35 +00:00
Properly handle EOF in varint-rs (#106)
This commit is contained in:
@ -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> {
|
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
|
||||||
loop {
|
loop {
|
||||||
if src.is_empty() && self.state.is_some() {
|
if src.is_empty() {
|
||||||
break Err(io::Error::from(io::ErrorKind::UnexpectedEof));
|
if self.state.is_some() {
|
||||||
|
break Err(io::Error::from(io::ErrorKind::UnexpectedEof));
|
||||||
|
} else {
|
||||||
|
break Ok(None);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// We know that the length is not 0, so this cannot fail.
|
// We know that the length is not 0, so this cannot fail.
|
||||||
let first_byte = src.split_to(1)[0];
|
let first_byte = src.split_to(1)[0];
|
||||||
@ -633,4 +637,12 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(result.unwrap_err().kind(), io::ErrorKind::UnexpectedEof);
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user