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

Decoder's std::io::Read::read returns error when given a 0-length buffer #318

Open
tliron opened this issue Jan 18, 2025 · 0 comments
Open

Comments

@tliron
Copy link

tliron commented Jan 18, 2025

It is uncommon, but possible, for a higher-level reader to provide a 0-length buffer. According to the documentation the function should then return Ok(0). Unfortunately this is not the case with zstd.

Here's a quick way to reproduce:

use {
    std::{fs::*, io::*},
    zstd::*,
};

pub fn main() {
    let source = File::open("work/example.zst").unwrap();
    let mut decoder = Decoder::new(source).unwrap();

    let mut buf: [u8; 0] = []; // empty buffers are allowed
    let completed = decoder.read(&mut buf).unwrap();
    println!("{}", completed);
}

The error is indeed rather unhelpful. :)

{ kind: Other, error: "Operation made no progress over multiple calls, due to output buffer being full" }

It should be a very easy fix! Something like this:

impl<ReadT> io::Read for Decoder<ReadT>
where
    ReadT: io::Read,
{
    fn read(&mut self, mut buf: &mut [u8]) -> io::Result<usize> {
        if buf.len() == 0 {
            return Ok(0);
        }
       ....
    }
}
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

1 participant