zig icon indicating copy to clipboard operation
zig copied to clipboard

std.compress.flate: fix panic when reading into empty buffer

Open pavelverigo opened this issue 1 year ago • 2 comments

Fixes: #19895

Calling .read will result in panic when reading into an empty buffer. This happens, because limit: usize parameter in .get function have alternative meaning, for limit=0.

Note: I encountered this bug, when using tar iterator on top of gzip reader.

@ianic, while adding test for this bug, I found there are no simple tests for public facing functions of Inflate struct, maybe in future revision or maintenance they could be added with my test merged into them, thank you.

pavelverigo avatar May 08 '24 14:05 pavelverigo

Great fix, thanks.

Public interface tests are located in flate.zig

You can put this test there, changing var decomp = std.compress.flate.decompressor(in.reader()); to use local decompressor function.

test "bug 19895" {
    const input = &[_]u8{
        0b0000_0001, 0b0000_1100, 0x00, 0b1111_0011, 0xff, // deflate fixed buffer header len, nlen
        'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 0x0a, // non compressed data
    };
    var in = std.io.fixedBufferStream(input);
    var decomp = decompressor(in.reader());
    var buf: [0]u8 = undefined;
    try testing.expectEqual(0, try decomp.read(&buf));
}

but I like it more where it is inline with previous bug test fix. I'll suggest change in that same line from: var decomp = std.compress.flate.decompressor(in.reader()); to: var decomp = decompressor(.raw, in.reader()); to be clear that we are testing function from current file.

ianic avatar May 08 '24 21:05 ianic

I changed the line to var decomp = decompressor(.raw, in.reader());

pavelverigo avatar May 08 '24 21:05 pavelverigo