Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions crates/async-compression/src/generic/bufread/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::{
codecs::DecodeV2,
core::util::{PartialBuffer, WriteBuffer},
};

use std::{io::Result, ops::ControlFlow};

#[derive(Debug)]
Expand All @@ -11,6 +10,7 @@ enum State {
Flushing,
Done,
Next,
Error(std::io::Error),
}

#[derive(Debug)]
Expand Down Expand Up @@ -54,7 +54,14 @@ impl Decoder {
Ok(true) => State::Flushing,
// ignore the first error, occurs when input is empty
// but we need to run decode to flush
Err(err) if !first => return ControlFlow::Break(Err(err)),
Err(err) if !first => {
self.state = State::Error(err);
if output.written_len() > 0 {
return ControlFlow::Break(Ok(()));
} else {
continue;
}
}
// poll for more data for the next decode
_ => break,
}
Expand All @@ -66,7 +73,12 @@ impl Decoder {
Ok(true) => {
if self.multiple_members {
if let Err(err) = decoder.reinit() {
return ControlFlow::Break(Err(err));
self.state = State::Error(err);
if output.written_len() > 0 {
return ControlFlow::Break(Ok(()));
} else {
continue;
}
}

// The decode stage might consume all the input,
Expand All @@ -78,7 +90,14 @@ impl Decoder {
}
}
Ok(false) => State::Flushing,
Err(err) => return ControlFlow::Break(Err(err)),
Err(err) => {
self.state = State::Error(err);
if output.written_len() > 0 {
return ControlFlow::Break(Ok(()));
} else {
continue;
}
}
}
}

Expand All @@ -95,6 +114,13 @@ impl Decoder {
State::Decoding
}
}

State::Error(_) => {
let State::Error(err) = std::mem::replace(&mut self.state, State::Done) else {
unreachable!()
};
return ControlFlow::Break(Err(err));
}
};

if output.has_no_spare_space() {
Expand Down
33 changes: 30 additions & 3 deletions crates/async-compression/src/generic/bufread/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ enum State {
Flushing,
Finishing,
Done,
Error(std::io::Error),
}

#[derive(Debug)]
Expand Down Expand Up @@ -53,7 +54,12 @@ impl Encoder {
State::Finishing
} else {
if let Err(err) = encoder.encode(input, output) {
return ControlFlow::Break(Err(err));
self.state = State::Error(err);
if output.written_len() > 0 {
return ControlFlow::Break(Ok(()));
} else {
continue;
}
}

*read += input.written().len();
Expand All @@ -72,16 +78,37 @@ impl Encoder {
break;
}
Ok(false) => State::Flushing,
Err(err) => return ControlFlow::Break(Err(err)),
Err(err) => {
self.state = State::Error(err);
if output.written_len() > 0 {
return ControlFlow::Break(Ok(()));
} else {
continue;
}
}
},

State::Finishing => match encoder.finish(output) {
Ok(true) => State::Done,
Ok(false) => State::Finishing,
Err(err) => return ControlFlow::Break(Err(err)),
Err(err) => {
self.state = State::Error(err);
if output.written_len() > 0 {
return ControlFlow::Break(Ok(()));
} else {
continue;
}
}
},

State::Done => return ControlFlow::Break(Ok(())),

State::Error(_) => {
let State::Error(err) = std::mem::replace(&mut self.state, State::Done) else {
unreachable!()
};
return ControlFlow::Break(Err(err));
}
};

if output.has_no_spare_space() {
Expand Down
21 changes: 21 additions & 0 deletions crates/async-compression/tests/gzip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,24 @@ fn gzip_bufread_chunks_compress_flushes_when_reader_pending() {
chunks_received.load(Ordering::Relaxed)
);
}

#[test]
#[ntest::timeout(1000)]
#[cfg(feature = "futures-io")]
fn gzip_bufread_chunks_decompress_without_footer_emits_all_payload() {
use flate2::bufread::GzDecoder;
use std::io::Read;

let mut bytes = compress_with_header(&[1, 2, 3, 4, 5, 6]);

// Remove the footer.
bytes.truncate(bytes.len() - 8);

let mut decoder = GzDecoder::new(bytes.as_slice());

let mut output = vec![];
let result = decoder.read_to_end(&mut output);

assert!(result.is_err());
assert_eq!(output, &[1, 2, 3, 4, 5, 6][..]);
}