mirror of
https://github.com/ziglang/zig.git
synced 2025-12-27 00:23:22 +00:00
zstd.Decompress: Treat a partial magic number as a failure
Previously, the "allow EndOfStream" part of this logic was too permissive. If there are a few dangling bytes at the end of the stream, that should be treated as a bad magic number. The only case where EndOfStream is allowed is when the stream is truly at the end, with exactly zero bytes available.
This commit is contained in:
parent
59de7e3a57
commit
60b0b21296
@ -121,6 +121,12 @@ test Decompress {
|
|||||||
try testExpectDecompress(uncompressed, compressed19);
|
try testExpectDecompress(uncompressed, compressed19);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test "partial magic number" {
|
||||||
|
const input_raw =
|
||||||
|
"\x28\xb5\x2f"; // 3 bytes of the 4-byte zstandard frame magic number
|
||||||
|
try testExpectDecompressError(error.BadMagic, input_raw);
|
||||||
|
}
|
||||||
|
|
||||||
test "zero sized raw block" {
|
test "zero sized raw block" {
|
||||||
const input_raw =
|
const input_raw =
|
||||||
"\x28\xb5\x2f\xfd" ++ // zstandard frame magic number
|
"\x28\xb5\x2f\xfd" ++ // zstandard frame magic number
|
||||||
|
|||||||
@ -158,7 +158,18 @@ fn stream(r: *Reader, w: *Writer, limit: Limit) Reader.StreamError!usize {
|
|||||||
|
|
||||||
switch (d.state) {
|
switch (d.state) {
|
||||||
.new_frame => {
|
.new_frame => {
|
||||||
// Allow error.EndOfStream only on the frame magic.
|
// Only return EndOfStream when there are exactly 0 bytes remaining on the
|
||||||
|
// frame magic. Any partial magic bytes should be considered a failure.
|
||||||
|
in.fill(@sizeOf(Frame.Magic)) catch |err| switch (err) {
|
||||||
|
error.EndOfStream => {
|
||||||
|
if (in.bufferedLen() != 0) {
|
||||||
|
d.err = error.BadMagic;
|
||||||
|
return error.ReadFailed;
|
||||||
|
}
|
||||||
|
return err;
|
||||||
|
},
|
||||||
|
else => |e| return e,
|
||||||
|
};
|
||||||
const magic = try in.takeEnumNonexhaustive(Frame.Magic, .little);
|
const magic = try in.takeEnumNonexhaustive(Frame.Magic, .little);
|
||||||
initFrame(d, w.buffer.len, magic) catch |err| {
|
initFrame(d, w.buffer.len, magic) catch |err| {
|
||||||
d.err = err;
|
d.err = err;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user