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

Conversation

naufraghi
Copy link

In write::BzDecoder::try_finish do not loop if nothing was written

Closes #96

naufraghi and others added 2 commits December 11, 2024 16:03
In `write::BzDecoder::try_finish` do not loop if nothing was written

Closes trifectatechfoundation#96
@bjorn3 bjorn3 force-pushed the 96-infinite-loop-writing-empty-buffer branch from 1db557d to cccce2f Compare December 11, 2024 15:09
@bjorn3
Copy link
Collaborator

bjorn3 commented Dec 11, 2024

Rebased to fix a merge conflict and added a test.

@bjorn3 bjorn3 requested a review from folkertdev December 11, 2024 15:09
@@ -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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Infinite loop writing an empty buffer in write::BzDecoder
3 participants