Skip to content

Commit

Permalink
Don't allow writing to finished Encoder
Browse files Browse the repository at this point in the history
If an Encoder is finished, it will swallow writes without error but never let you flush or finish those.
  • Loading branch information
anacrolix committed Aug 17, 2024
1 parent d698082 commit 74d7e23
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/stream/read/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl<'a, R: BufRead> Decoder<'a, R> {
/// The prefix must be the same as the one used during compression.
pub fn with_ref_prefix<'b>(
reader: R,
ref_prefix: &'b [u8]
ref_prefix: &'b [u8],
) -> io::Result<Self>
where
'b: 'a,
Expand Down
9 changes: 9 additions & 0 deletions src/stream/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,12 @@ fn reader_to_writer() {

assert_eq!(clear, &decompressed_buffer[..]);
}

#[test]
fn test_finish_empty_encoder() {
use std::io::Write;
let mut enc = Encoder::new(Vec::new(), 0).unwrap();
enc.do_finish().unwrap();
enc.write_all(b"this should not work").unwrap_err();
enc.finish().unwrap();
}
6 changes: 6 additions & 0 deletions src/stream/zio/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,12 @@ where
D: Operation,
{
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
if self.finished {
return Err(io::Error::new(
io::ErrorKind::Other,
"encoder is finished",
));
}
// Keep trying until _something_ has been consumed.
// As soon as some input has been taken, we cannot afford
// to take any chance: if an error occurs, the user couldn't know
Expand Down

0 comments on commit 74d7e23

Please sign in to comment.