mirror of
https://github.com/tokio-rs/tokio.git
synced 2025-09-25 12:00:35 +00:00
codec: fix incorrect handling of invalid utf-8 in LinesCodec::decode_eof
(#7011)
This commit is contained in:
parent
b5c227d51f
commit
129f9fc0c8
@ -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())
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user