From 9c44e8f529977abac483a30617d7bc49400e9cbe Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Mon, 19 Aug 2024 23:39:39 +1000 Subject: [PATCH] Don't allow writing to finished Encoder (#297) If an Encoder is finished, it will swallow writes without error but never let you flush or finish those. --- src/stream/read/mod.rs | 2 +- src/stream/tests.rs | 9 +++++++++ src/stream/zio/writer.rs | 6 ++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/stream/read/mod.rs b/src/stream/read/mod.rs index 06e6022f..e107bd82 100644 --- a/src/stream/read/mod.rs +++ b/src/stream/read/mod.rs @@ -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 where 'b: 'a, diff --git a/src/stream/tests.rs b/src/stream/tests.rs index 7a77a67f..c783bb9e 100644 --- a/src/stream/tests.rs +++ b/src/stream/tests.rs @@ -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(); +} diff --git a/src/stream/zio/writer.rs b/src/stream/zio/writer.rs index de1c53dc..bb812be6 100644 --- a/src/stream/zio/writer.rs +++ b/src/stream/zio/writer.rs @@ -174,6 +174,12 @@ where D: Operation, { fn write(&mut self, buf: &[u8]) -> io::Result { + 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