flate: Handle invalid block type

Fixes `panic: invalid enum value` when the type bits had the u2 value of 3.

Contributes towards #24741
This commit is contained in:
Ryan Liptak 2025-08-08 01:37:59 -07:00 committed by Andrew Kelley
parent d984e7d2fa
commit 23fff3442d

View File

@ -31,6 +31,7 @@ const BlockType = enum(u2) {
stored = 0, stored = 0,
fixed = 1, fixed = 1,
dynamic = 2, dynamic = 2,
invalid = 3,
}; };
const State = union(enum) { const State = union(enum) {
@ -49,6 +50,7 @@ pub const Error = Container.Error || error{
InvalidCode, InvalidCode,
InvalidMatch, InvalidMatch,
WrongStoredBlockNlen, WrongStoredBlockNlen,
InvalidBlockType,
InvalidDynamicBlockHeader, InvalidDynamicBlockHeader,
ReadFailed, ReadFailed,
OversubscribedHuffmanTree, OversubscribedHuffmanTree,
@ -360,6 +362,7 @@ fn streamInner(d: *Decompress, w: *Writer, limit: std.Io.Limit) (Error || Reader
continue :sw .dynamic_block; continue :sw .dynamic_block;
}, },
.invalid => return error.InvalidBlockType,
} }
}, },
.stored_block => |remaining_len| { .stored_block => |remaining_len| {
@ -1141,6 +1144,10 @@ test "puff09" {
try testDecompress(.raw, @embedFile("testdata/fuzz/puff09.input"), "P"); try testDecompress(.raw, @embedFile("testdata/fuzz/puff09.input"), "P");
} }
test "invalid block type" {
try testFailure(.raw, &[_]u8{0b110}, error.InvalidBlockType);
}
test "bug 18966" { test "bug 18966" {
try testDecompress( try testDecompress(
.gzip, .gzip,