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

Flushing ZlibEncoder loops infinitely on some writers #47

Closed
HeroicKatora opened this issue May 18, 2020 · 2 comments
Closed

Flushing ZlibEncoder loops infinitely on some writers #47

HeroicKatora opened this issue May 18, 2020 · 2 comments

Comments

@HeroicKatora
Copy link
Member

HeroicKatora commented May 18, 2020

When the underlying writer only accepts very small write request the flush process of a ZlibEncoder appears to try and write the same data infinitely often.

Full reproduction source code
use std::io::{self, Write};

fn main() {
    let _ = deflate::write::ZlibEncoder::new(SmallWriter::new(vec![], 2), deflate::Compression::Fast).flush();
}

struct SmallWriter<W: Write> {
    writer: W,
    small: usize,
}

impl<W: Write> SmallWriter<W> {
    fn new(writer: W, buf_len: usize) -> SmallWriter<W> {
        SmallWriter {
            writer,
            small: buf_len,
        }
    }
}

impl<W: Write> Write for SmallWriter<W> {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        // Never write more than `small` bytes at a time.
        let small = buf.len().min(self.small);
        self.writer.write(&buf[..small])
    }

    fn flush(&mut self) -> io::Result<()> {
        Ok(())
    }
}
@HeroicKatora
Copy link
Member Author

Discovered in image-rs/image-png#206

@oyvindln
Copy link
Collaborator

I think it may be due to flush trying to output an empty block stored block (as per zlib sync flush) at the end when called, which won't fit in two bytes. Will try to find some way of solving it.

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

No branches or pull requests

2 participants