From e764e8827ca00018df173520761520d81e2f4693 Mon Sep 17 00:00:00 2001 From: Matteo Bertini Date: Mon, 29 May 2023 09:26:28 +0200 Subject: [PATCH 1/2] Avoid infinite loop writing and empty buffer In `write::BzDecoder::try_finish` do not loop if nothing was written Closes #96 --- src/write.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/write.rs b/src/write.rs index 5d80000e..f6d65dd1 100644 --- a/src/write.rs +++ b/src/write.rs @@ -203,7 +203,8 @@ impl BzDecoder { /// /// [`write`]: Self::write pub fn try_finish(&mut self) -> io::Result<()> { - while !self.done { + // If nothing was written, there is no need to loop + while !self.done && self.total_in() > 0 { let _ = self.write(&[])?; } self.dump() From cccce2f6a5e3b4c132f71484933d505e8d4e271c Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Wed, 11 Dec 2024 16:09:07 +0100 Subject: [PATCH 2/2] Add test for issue 96 --- src/write.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/write.rs b/src/write.rs index f6d65dd1..b96ee77c 100644 --- a/src/write.rs +++ b/src/write.rs @@ -310,6 +310,13 @@ mod tests { assert_eq!(&data[..], b""); } + #[test] + fn write_empty2() { + let mut d = BzDecoder::new(Vec::new()); + d.write(b"").unwrap(); + d.finish().unwrap(); + } + #[test] fn qc() { ::quickcheck::quickcheck(test as fn(_) -> _);