Skip to content

Commit 0e61592

Browse files
author
dweiller
committed
std.compress.zstandard: add window size limit param
1 parent 1436b6b commit 0e61592

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

lib/std/compress/zstandard/decompress.zig

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,12 @@ pub fn decodeZStandardFrame(dest: []u8, src: []const u8, verify_checksum: bool)
562562
/// `decodeZStandardFrame()`. Returns `error.WindowSizeUnknown` if the frame
563563
/// does not declare its content size or a window descriptor (this indicates a
564564
/// malformed frame).
565-
pub fn decodeZStandardFrameAlloc(allocator: std.mem.Allocator, src: []const u8, verify_checksum: bool) ![]u8 {
565+
pub fn decodeZStandardFrameAlloc(
566+
allocator: std.mem.Allocator,
567+
src: []const u8,
568+
verify_checksum: bool,
569+
window_size_max: usize,
570+
) ![]u8 {
566571
var result = std.ArrayList(u8).init(allocator);
567572
assert(readInt(u32, src[0..4]) == frame.ZStandard.magic_number);
568573
var consumed_count: usize = 4;
@@ -572,7 +577,10 @@ pub fn decodeZStandardFrameAlloc(allocator: std.mem.Allocator, src: []const u8,
572577
if (frame_header.descriptor.dictionary_id_flag != 0) return error.DictionaryIdFlagUnsupported;
573578

574579
const window_size_raw = frameWindowSize(frame_header) orelse return error.WindowSizeUnknown;
575-
const window_size = std.math.cast(usize, window_size_raw) orelse return error.WindowTooLarge;
580+
const window_size = if (window_size_raw > window_size_max)
581+
return error.WindowTooLarge
582+
else
583+
@intCast(usize, window_size_raw);
576584

577585
const should_compute_checksum = frame_header.descriptor.content_checksum_flag and verify_checksum;
578586
var hash = if (should_compute_checksum) std.hash.XxHash64.init(0) else null;

0 commit comments

Comments
 (0)