From 5756a005a646108bfdc386f48385803303c372a8 Mon Sep 17 00:00:00 2001 From: r-zig Date: Fri, 26 Feb 2021 00:21:59 +0200 Subject: [PATCH] codec: LinesCodec should only return MaxLineLengthExceeded once per line (#3556) --- tokio-util/src/codec/lines_codec.rs | 2 +- tokio-util/tests/codecs.rs | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/tokio-util/src/codec/lines_codec.rs b/tokio-util/src/codec/lines_codec.rs index e1816d8c4..4c5c2e6db 100644 --- a/tokio-util/src/codec/lines_codec.rs +++ b/tokio-util/src/codec/lines_codec.rs @@ -133,7 +133,7 @@ impl Decoder for LinesCodec { buf.advance(read_to); self.next_index = 0; if buf.is_empty() { - return Err(LinesCodecError::MaxLineLengthExceeded); + return Ok(None); } } (false, Some(offset)) => { diff --git a/tokio-util/tests/codecs.rs b/tokio-util/tests/codecs.rs index 89d8a47a9..aae612938 100644 --- a/tokio-util/tests/codecs.rs +++ b/tokio-util/tests/codecs.rs @@ -201,7 +201,26 @@ fn lines_decoder_discard_repeat() { buf.put_slice(b"aa"); assert!(codec.decode(buf).is_err()); buf.put_slice(b"a"); + assert_eq!(None, codec.decode(buf).unwrap()); +} + +// Regression test for [subsequent calls to LinesCodec decode does not return the desired results bug](https://github.com/tokio-rs/tokio/issues/3555) +#[test] +fn lines_decoder_max_length_underrun_twice() { + const MAX_LENGTH: usize = 11; + + let mut codec = LinesCodec::new_with_max_length(MAX_LENGTH); + let buf = &mut BytesMut::new(); + + buf.reserve(200); + buf.put_slice(b"line "); + assert_eq!(None, codec.decode(buf).unwrap()); + buf.put_slice(b"too very l"); assert!(codec.decode(buf).is_err()); + buf.put_slice(b"aaaaaaaaaaaaaaaaaaaaaaa"); + assert_eq!(None, codec.decode(buf).unwrap()); + buf.put_slice(b"ong\nshort\n"); + assert_eq!("short", codec.decode(buf).unwrap().unwrap()); } #[test]