Commit fa4a626fac

Pavel Verigo <58272683+pavelverigo@users.noreply.github.com>
2024-05-08 16:29:29
std.compress.flate: fix panic when reading into empty buffer
1 parent 33c3bf0
Changed files (1)
lib
std
compress
lib/std/compress/flate/inflate.zig
@@ -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.
         /// End of stream is not an error condition.
         pub fn read(self: *Self, buffer: []u8) Error!usize {
+            if (buffer.len == 0) return 0;
             const out = try self.get(buffer.len);
             @memcpy(buffer[0..out.len], out);
             return out.len;
@@ -556,3 +557,14 @@ test "bug 18966" {
     try decompress(.gzip, in.reader(), out.writer());
     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));
+}