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

This commit is contained in:
Pavel Verigo 2024-05-08 16:29:29 +02:00 committed by Andrew Kelley
parent 68629fedef
commit d4d1efeb3e

View File

@ -347,6 +347,7 @@ pub fn Inflate(comptime container: Container, comptime LookaheadType: type, comp
/// If the number of bytes read is 0, it means end of stream. /// If the number of bytes read is 0, it means end of stream.
/// End of stream is not an error condition. /// End of stream is not an error condition.
pub fn read(self: *Self, buffer: []u8) Error!usize { pub fn read(self: *Self, buffer: []u8) Error!usize {
if (buffer.len == 0) return 0;
const out = try self.get(buffer.len); const out = try self.get(buffer.len);
@memcpy(buffer[0..out.len], out); @memcpy(buffer[0..out.len], out);
return out.len; return out.len;
@ -556,3 +557,14 @@ test "bug 18966" {
try decompress(.gzip, in.reader(), out.writer()); try decompress(.gzip, in.reader(), out.writer());
try testing.expectEqualStrings(expect, out.items); try testing.expectEqualStrings(expect, out.items);
} }
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(.raw, in.reader());
var buf: [0]u8 = undefined;
try testing.expectEqual(0, try decomp.read(&buf));
}