std.compress.zstd: better error code for oversize literals

This commit is contained in:
Andrew Kelley 2025-05-01 15:41:30 -07:00
parent 2ff5ea2231
commit 2dbf05af0a
2 changed files with 3 additions and 7 deletions

View File

@ -110,8 +110,6 @@ fn testExpectDecompressError(err: anyerror, compressed: []const u8) !void {
var zstd_stream: Decompress = .init(&in, .{});
try std.testing.expectError(error.ReadFailed, zstd_stream.reader().readRemainingArrayList(gpa, null, &out, .unlimited));
try std.testing.expectError(err, zstd_stream.err orelse {});
return error.TestFailed;
}
test "decompression" {
@ -150,5 +148,5 @@ test "declared raw literals size too large" {
// Note that the regenerated_size in the above input is larger than block maximum size, so the
// block can't be valid as it is a raw literals block.
try testExpectDecompressError(error.MalformedBlock, input_raw);
try testExpectDecompressError(error.MalformedLiteralsSection, input_raw);
}

View File

@ -37,7 +37,6 @@ pub const Error = error{
EndOfStream,
HuffmanTreeIncomplete,
InvalidBitStream,
LiteralsBufferUndersize,
MalformedAccuracyLog,
MalformedBlock,
MalformedCompressedBlock,
@ -1224,7 +1223,6 @@ pub const LiteralsSection = struct {
/// Not enough bytes to complete the section.
EndOfStream,
ReadFailed,
LiteralsBufferUndersize,
MissingStartBit,
};
@ -1232,7 +1230,7 @@ pub const LiteralsSection = struct {
const header = try Header.decode(in, remaining);
switch (header.block_type) {
.raw => {
if (buffer.len < header.regenerated_size) return error.LiteralsBufferUndersize;
if (buffer.len < header.regenerated_size) return error.MalformedLiteralsSection;
remaining.* = remaining.subtract(header.regenerated_size) orelse return error.EndOfStream;
try in.readSlice(buffer[0..header.regenerated_size]);
return .{
@ -1259,7 +1257,7 @@ pub const LiteralsSection = struct {
const huffman_tree_size = @intFromEnum(before_remaining) - @intFromEnum(remaining.*);
const total_streams_size = std.math.sub(usize, header.compressed_size.?, huffman_tree_size) catch
return error.MalformedLiteralsSection;
if (total_streams_size > buffer.len) return error.LiteralsBufferUndersize;
if (total_streams_size > buffer.len) return error.MalformedLiteralsSection;
remaining.* = remaining.subtract(total_streams_size) orelse return error.EndOfStream;
try in.readSlice(buffer[0..total_streams_size]);
const stream_data = buffer[0..total_streams_size];