Skip to content

Commit

Permalink
add lengths overflow test case
Browse files Browse the repository at this point in the history
Fixes #1
  • Loading branch information
ianic committed Feb 8, 2024
1 parent 2c70a38 commit 40a5b75
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/inflate.zig
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pub fn Inflate(comptime container: Container, comptime ReaderType: type) type {
DeflateInvalidCode,
DeflateInvalidBlockType,
DeflateWrongNlen,
CorruptInput,
};

pub fn init(rt: ReaderType) Self {
Expand Down Expand Up @@ -186,6 +187,7 @@ pub fn Inflate(comptime container: Container, comptime ReaderType: type) type {
return 1;
},
16 => {
if (pos == 0) return error.CorruptInput;
// Copy the previous code length 3 - 6 times.
// The next 2 bits indicate repeat length
const n: u8 = @as(u8, try self.bits.read(u2)) + 3;
Expand Down Expand Up @@ -470,3 +472,13 @@ test "zlib decompress" {
try testing.expectEqualStrings(c.out, al.items);
}
}

test "lengths overflow" {
const data = "\xed\x1d$\xe9\xff\xff9\x0e";

var fb = std.io.fixedBufferStream(data);
var al = std.ArrayList(u8).init(testing.allocator);
defer al.deinit();

try testing.expectError(error.CorruptInput, decompress(.raw, fb.reader(), al.writer()));
}

0 comments on commit 40a5b75

Please sign in to comment.