zstd-rs icon indicating copy to clipboard operation
zstd-rs copied to clipboard

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

Open tliron opened this issue 1 year ago • 0 comments

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);
        }
       ....
    }
}

tliron avatar Jan 18 '25 05:01 tliron