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

Avoid infinite loop writing and empty buffer (in write::BzDecoder) #97

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 9 additions & 1 deletion src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ impl<W: Write> BzDecoder<W> {
///
/// [`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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this should actually return an error when self.total_in() == 0? An empty file is not a valid bzip2 file.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know, should we return error if we try_finish a writer without ever writing anything? I think maybe yes, we should at least pass in the empty bzip2 payload. If we didn't, we have a logic error somewhere on the caller side.

Considering that the code path was "protected" by an infinite loop, returning an error is not a breaking change 😄

That said, I completely forgot the context in which I made the PR 😞

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

returning an error on an empty input sounds like the best approach to me. The doc comment should reflect that change

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm although doesn't that break the write_empty test below? also what happens when you write just part of a bzip file (e.g. the first 2 bytes of the header) and then flush? That behavior should be consistent.

let _ = self.write(&[])?;
}
self.dump()
Expand Down Expand Up @@ -309,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(_) -> _);
Expand Down
Loading