Skip to content

Commit d4d1efe

Browse files
pavelverigoandrewrk
authored andcommitted
std.compress.flate: fix panic when reading into empty buffer
1 parent 68629fe commit d4d1efe

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

lib/std/compress/flate/inflate.zig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ pub fn Inflate(comptime container: Container, comptime LookaheadType: type, comp
347347
/// If the number of bytes read is 0, it means end of stream.
348348
/// End of stream is not an error condition.
349349
pub fn read(self: *Self, buffer: []u8) Error!usize {
350+
if (buffer.len == 0) return 0;
350351
const out = try self.get(buffer.len);
351352
@memcpy(buffer[0..out.len], out);
352353
return out.len;
@@ -556,3 +557,14 @@ test "bug 18966" {
556557
try decompress(.gzip, in.reader(), out.writer());
557558
try testing.expectEqualStrings(expect, out.items);
558559
}
560+
561+
test "bug 19895" {
562+
const input = &[_]u8{
563+
0b0000_0001, 0b0000_1100, 0x00, 0b1111_0011, 0xff, // deflate fixed buffer header len, nlen
564+
'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 0x0a, // non compressed data
565+
};
566+
var in = std.io.fixedBufferStream(input);
567+
var decomp = decompressor(.raw, in.reader());
568+
var buf: [0]u8 = undefined;
569+
try testing.expectEqual(0, try decomp.read(&buf));
570+
}

0 commit comments

Comments
 (0)