From 129f9fc0c8e9787dee3db5a6f65c3370ed148f7e Mon Sep 17 00:00:00 2001 From: Zettroke Date: Wed, 4 Dec 2024 13:18:14 +0300 Subject: [PATCH] codec: fix incorrect handling of invalid utf-8 in `LinesCodec::decode_eof` (#7011) --- tokio-util/src/codec/lines_codec.rs | 2 +- tokio-util/tests/codecs.rs | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/tokio-util/src/codec/lines_codec.rs b/tokio-util/src/codec/lines_codec.rs index 47f68d88fda..cc1ac4ebdad 100644 --- a/tokio-util/src/codec/lines_codec.rs +++ b/tokio-util/src/codec/lines_codec.rs @@ -169,6 +169,7 @@ impl Decoder for LinesCodec { Ok(match self.decode(buf)? { Some(frame) => Some(frame), None => { + self.next_index = 0; // No terminating newline - return remaining data, if any if buf.is_empty() || buf == &b"\r"[..] { None @@ -176,7 +177,6 @@ impl Decoder for LinesCodec { let line = buf.split_to(buf.len()); let line = without_carriage_return(&line); let line = utf8(line)?; - self.next_index = 0; Some(line.to_string()) } } diff --git a/tokio-util/tests/codecs.rs b/tokio-util/tests/codecs.rs index 1cfd9208e86..57743be7155 100644 --- a/tokio-util/tests/codecs.rs +++ b/tokio-util/tests/codecs.rs @@ -62,6 +62,19 @@ fn lines_decoder() { assert_eq!(None, codec.decode_eof(buf).unwrap()); } +#[test] +fn lines_decoder_invalid_utf8() { + let mut codec = LinesCodec::new(); + let buf = &mut BytesMut::new(); + buf.reserve(200); + buf.put_slice(b"line 1\xc3\x28"); + assert_eq!(None, codec.decode(buf).unwrap()); + assert!(codec.decode_eof(buf).is_err()); + assert_eq!(None, codec.decode_eof(buf).unwrap()); + buf.put_slice(b"line 22222222222222\n"); + assert_eq!("line 22222222222222", codec.decode(buf).unwrap().unwrap()); +} + #[test] fn lines_decoder_max_length() { const MAX_LENGTH: usize = 6;