From cb122d412f3fc03b9fb704feb73a8957b652b148 Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Wed, 16 Oct 2024 00:25:22 +1100 Subject: [PATCH 1/3] Fix panic: cannot consume from pending buffer Fixed #298 Signed-off-by: Jiahao XU --- src/futures/bufread/generic/decoder.rs | 7 ++++--- src/tokio/bufread/generic/decoder.rs | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/futures/bufread/generic/decoder.rs b/src/futures/bufread/generic/decoder.rs index 123d90f..8c10b19 100644 --- a/src/futures/bufread/generic/decoder.rs +++ b/src/futures/bufread/generic/decoder.rs @@ -88,7 +88,7 @@ impl Decoder { State::Flushing } else { let mut input = PartialBuffer::new(input); - let done = this.decoder.decode(&mut input, output).or_else(|err| { + let res = this.decoder.decode(&mut input, output).or_else(|err| { // ignore the first error, occurs when input is empty // but we need to run decode to flush if first { @@ -96,13 +96,14 @@ impl Decoder { } else { Err(err) } - })?; + }); first = false; let len = input.written().len(); this.reader.as_mut().consume(len); - if done { + + if res? { State::Flushing } else { State::Decoding diff --git a/src/tokio/bufread/generic/decoder.rs b/src/tokio/bufread/generic/decoder.rs index 522835c..b3ab043 100644 --- a/src/tokio/bufread/generic/decoder.rs +++ b/src/tokio/bufread/generic/decoder.rs @@ -88,7 +88,7 @@ impl Decoder { State::Flushing } else { let mut input = PartialBuffer::new(input); - let done = this.decoder.decode(&mut input, output).or_else(|err| { + let res = this.decoder.decode(&mut input, output).or_else(|err| { // ignore the first error, occurs when input is empty // but we need to run decode to flush if first { @@ -96,13 +96,14 @@ impl Decoder { } else { Err(err) } - })?; + }); first = false; let len = input.written().len(); this.reader.as_mut().consume(len); - if done { + + if res? { State::Flushing } else { State::Decoding From eb076120713c3f9582450d658d2f1a55960722ea Mon Sep 17 00:00:00 2001 From: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> Date: Thu, 17 Oct 2024 00:43:07 +1100 Subject: [PATCH 2/3] Do not consume buf on first flush tokio bufread decoder impl Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> --- src/tokio/bufread/generic/decoder.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/tokio/bufread/generic/decoder.rs b/src/tokio/bufread/generic/decoder.rs index b3ab043..c4fdb98 100644 --- a/src/tokio/bufread/generic/decoder.rs +++ b/src/tokio/bufread/generic/decoder.rs @@ -98,10 +98,12 @@ impl Decoder { } }); - first = false; + if !first { + let len = input.written().len(); + this.reader.as_mut().consume(len); + } - let len = input.written().len(); - this.reader.as_mut().consume(len); + first = false; if res? { State::Flushing From 241bee6972d38b21ea51c71a3ebb95abb767ff89 Mon Sep 17 00:00:00 2001 From: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> Date: Thu, 17 Oct 2024 00:48:12 +1100 Subject: [PATCH 3/3] Do not advance buffer in first flush in futures bufread decoder impl Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> --- src/futures/bufread/generic/decoder.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/futures/bufread/generic/decoder.rs b/src/futures/bufread/generic/decoder.rs index 8c10b19..e97f0fc 100644 --- a/src/futures/bufread/generic/decoder.rs +++ b/src/futures/bufread/generic/decoder.rs @@ -98,10 +98,12 @@ impl Decoder { } }); - first = false; + if !first { + let len = input.written().len(); + this.reader.as_mut().consume(len); + } - let len = input.written().len(); - this.reader.as_mut().consume(len); + first = false; if res? { State::Flushing