authorgravatar for 4678790+dweiller@users.noreply.github.comDominic <4678790+dweiller@users.noreply.github.com> 2023-02-06 13:20:23+11:00
committergravatar for 4678790+dweiller@users.noreply.github.comDominic <4678790+dweiller@users.noreply.github.com> 2023-02-20 09:09:06+11:00
log2134769436f68547a5c1d93184a10fab03085b2a
treef88079ee4832454362cd4b4c43890f24119495ac
parent98bbd959b08db4d66a9ae68ce3fe4196594c6d9e

std.compress.zstandard: validate skippable frame size


1 files changed, 30 insertions(+), 14 deletions(-)

lib/std/compress/zstandard/decompress.zig+30-14
......@@ -107,19 +107,27 @@ pub fn decodeAlloc(
107107/// - `error.UnusedBitSet` if the unused bit of the frame header is set
108108/// - `error.EndOfStream` if `src` does not contain a complete frame
109109/// - an error in `block.Error` if there are errors decoding a block
110/// - `error.SkippableSizeTooLarge` if the frame is skippable and reports a
111/// size greater than `src.len`
110112pub fn decodeFrame(
111113 dest: []u8,
112114 src: []const u8,
113115 verify_checksum: bool,
114116) !ReadWriteCount {
115117 var fbs = std.io.fixedBufferStream(src);
116 return switch (try decodeFrameType(fbs.reader())) {
117 .zstandard => decodeZstandardFrame(dest, src, verify_checksum),
118 .skippable => ReadWriteCount{
119 .read_count = try fbs.reader().readIntLittle(u32) + 8,
120 .write_count = 0,
118 switch (try decodeFrameType(fbs.reader())) {
119 .zstandard => return decodeZstandardFrame(dest, src, verify_checksum),
120 .skippable => {
121 const content_size = try fbs.reader().readIntLittle(u32);
122 if (content_size > std.math.maxInt(usize) - 8) return error.SkippableSizeTooLarge;
123 const read_count = @as(usize, content_size) + 8;
124 if (read_count > src.len) return error.SkippableSizeTooLarge;
125 return ReadWriteCount{
126 .read_count = read_count,
127 .write_count = 0,
128 };
121129 },
122 };
130 }
123131}
124132
125133pub const DecodeResult = struct {
......@@ -150,6 +158,8 @@ pub const DecodedFrame = union(enum) {
150158/// - `error.EndOfStream` if `src` does not contain a complete frame
151159/// - `error.OutOfMemory` if `allocator` cannot allocate enough memory
152160/// - an error in `block.Error` if there are errors decoding a block
161/// - `error.SkippableSizeTooLarge` if the frame is skippable and reports a
162/// size greater than `src.len`
153163pub fn decodeFrameAlloc(
154164 allocator: Allocator,
155165 src: []const u8,
......@@ -159,17 +169,23 @@ pub fn decodeFrameAlloc(
159169 var fbs = std.io.fixedBufferStream(src);
160170 const reader = fbs.reader();
161171 const magic = try reader.readIntLittle(u32);
162 return switch (try frameType(magic)) {
163 .zstandard => .{
172 switch (try frameType(magic)) {
173 .zstandard => return .{
164174 .zstandard = try decodeZstandardFrameAlloc(allocator, src, verify_checksum, window_size_max),
165175 },
166 .skippable => .{
167 .skippable = .{
168 .magic_number = magic,
169 .frame_size = try reader.readIntLittle(u32),
170 },
176 .skippable => {
177 const content_size = try fbs.reader().readIntLittle(u32);
178 if (content_size > std.math.maxInt(usize) - 8) return error.SkippableSizeTooLarge;
179 const read_count = @as(usize, content_size) + 8;
180 if (read_count > src.len) return error.SkippableSizeTooLarge;
181 return .{
182 .skippable = .{
183 .magic_number = magic,
184 .frame_size = content_size,
185 },
186 };
171187 },
172 };
188 }
173189}
174190
175191/// Returns the frame checksum corresponding to the data fed into `hasher`