Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

write::BzDecoder: Fix infinite loop on drop when no data is read or written #118

Merged
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
BZDecoder::drop: add test for it not causing an infinite loop
folkertdev committed Dec 16, 2024

Verified

This commit was signed with the committer’s verified signature.
folkertdev Folkert de Vries
commit 51ddb381bfcc5c1fe24b6bbbdbe97a3b86302013
20 changes: 13 additions & 7 deletions src/write.rs
Original file line number Diff line number Diff line change
@@ -204,14 +204,12 @@ impl<W: Write> BzDecoder<W> {
/// [`write`]: Self::write
pub fn try_finish(&mut self) -> io::Result<()> {
while !self.done {
let before = self.total_in();
let written = self.write(&[])?;
let read_before = self.total_in();
let written_now = self.write(&[])?;

if self.total_in() == before && written == 0 {
return Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
"Input EOF reached before logical stream ends",
));
if self.total_in() == read_before && written_now == 0 {
let msg = "Input EOF reached before logical stream ends";
return Err(io::Error::new(io::ErrorKind::UnexpectedEof, msg));
}
}
self.dump()
@@ -319,6 +317,14 @@ mod tests {
assert_eq!(&data[..], b"");
}

#[test]
fn write_empty_drop() {
// the drop implementation used to loop infinitely for empty input
// see https://github.com/trifectatechfoundation/bzip2-rs/pull/118
let d = BzDecoder::new(Vec::new());
drop(d);
}

#[test]
fn write_invalid() {
// see https://github.com/trifectatechfoundation/bzip2-rs/issues/98