Skip to content

Commit

Permalink
fix line-codec not resetting state when hitting error in decode_eof
Browse files Browse the repository at this point in the history
  • Loading branch information
Zettroke committed Dec 4, 2024
1 parent b5c227d commit 6e2962f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tokio-util/src/codec/lines_codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,14 @@ 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
} else {
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())
}
}
Expand Down
13 changes: 13 additions & 0 deletions tokio-util/tests/codecs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 6e2962f

Please sign in to comment.