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

Infinite loop writing an empty buffer in write::BzDecoder #96

Open
naufraghi opened this issue May 11, 2023 · 0 comments · May be fixed by #97
Open

Infinite loop writing an empty buffer in write::BzDecoder #96

naufraghi opened this issue May 11, 2023 · 0 comments · May be fixed by #97

Comments

@naufraghi
Copy link

This minimal example is never returning with the current master (3032f37).

extern crate bzip2;

use bzip2::write::BzDecoder;
use std::io::prelude::*;

fn main() {
    let mut d = BzDecoder::new(Vec::new());
    d.write(b"").unwrap();
    d.finish().unwrap();
}

With a similar test case cargo test was stuck:

    #[test]
    fn write_empty() {
        let d = BzDecoder::new(Vec::new());
        let mut c = BzEncoder::new(d, ::Compression::default());
        c.write(b"").unwrap();
        let data = c.finish().unwrap().finish().unwrap();
        assert_eq!(&data[..], b"");
    }

Diving into the simpler example traceback the problem seems related to try_finish in write.rs, where we are writing an empty buffer forever:

    pub fn try_finish(&mut self) -> io::Result<()> {
        while !self.done {
            self.write(&[])?;
        }
        self.dump()
    }

Maybe it is sufficient to check if we received some input? Like:

    pub fn try_finish(&mut self) -> io::Result<()> {
        // If nothing entered, there is no need to loop
        while !self.done && self.total_in() != 0 {
            self.write(&[])?;
        }
        self.dump()
    }

That done cargo test works with the added test too.

Let me know if you'd like a Pull Request, it's ready on my disk.

naufraghi added a commit to naufraghi/bzip2-rs that referenced this issue May 29, 2023
In `write::BzDecoder::try_finish` do not loop if nothing was written

Closes alexcrichton#96
rtldg pushed a commit to srcwr/bzip2-rs that referenced this issue Mar 15, 2024
In `write::BzDecoder::try_finish` do not loop if nothing was written

Closes alexcrichton#96
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 a pull request may close this issue.

1 participant