Skip to content

Commit 640a351

Browse files
committed
fix: check for IDENTIFIER_FRAME at the start of the stream
1 parent 3ca6cf4 commit 640a351

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

src/frame.zig

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,13 @@ pub fn compress(allocator: std.mem.Allocator, bytes: []const u8) CompressError![
7575
///
7676
/// Caller owns the returned memory.
7777
pub fn uncompress(allocator: std.mem.Allocator, bytes: []const u8) UncompressError!?[]const u8 {
78-
std.debug.assert(bytes.len > 0);
79-
var slice = bytes;
78+
std.debug.assert(bytes.len > IDENTIFIER_FRAME.len);
79+
// Start of stream always starts with the `IDENTIFIER_FRAME`.
80+
if (!std.mem.eql(u8, bytes[0..IDENTIFIER_FRAME.len], &IDENTIFIER_FRAME)) {
81+
return UncompressError.BadIdentifier;
82+
}
83+
84+
var slice = bytes[IDENTIFIER_FRAME.len..];
8085

8186
var out = std.ArrayList(u8).init(allocator);
8287
errdefer out.deinit();
@@ -90,11 +95,6 @@ pub fn uncompress(allocator: std.mem.Allocator, bytes: []const u8) UncompressErr
9095
slice = slice[4 + frame_size ..];
9196

9297
switch (chunk_type) {
93-
.identifier => {
94-
if (!std.mem.eql(u8, frame, &IDENTIFIER)) {
95-
return UncompressError.BadIdentifier;
96-
}
97-
},
9898
.compressed => {
9999
const checksum = frame[0..4];
100100
const compressed = frame[4..];
@@ -114,7 +114,13 @@ pub fn uncompress(allocator: std.mem.Allocator, bytes: []const u8) UncompressErr
114114
if (crc(uncompressed) != std.mem.bytesToValue(u32, checksum)) return UncompressError.BadChecksum;
115115
try out.appendSlice(uncompressed);
116116
},
117-
.padding, .skippable => continue,
117+
.padding,
118+
.skippable,
119+
// The stream identifier chunk can come multiple times in the stream besides
120+
// the first; if such a chunk shows up, it should simply be ignored, assuming
121+
// it has the right length and contents.
122+
.identifier,
123+
=> continue,
118124
}
119125
}
120126

0 commit comments

Comments
 (0)