Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,14 @@ pub fn deserialize(T: type, serialized: []const u8, out: *T, allocator: ?Allocat
try deserialize(ptr.child, serialized[i * pitch .. (i + 1) * pitch], &out.*[i], allocator);
}
} else {
// Empty list of variable-size elements is encoded as zero bytes
// (no offset table is needed when there are no elements).
if (serialized.len == 0) {
if (allocator) |alloc| {
out.* = try alloc.alloc(ptr.child, 0);
}
return;
}
// read the first index, determine when the "variable size" list ends,
// and determine the size of the item as a result.
if (serialized.len < 4) return error.OffsetExceedsSize;
Expand Down
18 changes: 18 additions & 0 deletions src/tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2429,6 +2429,24 @@ test "deserialize struct: fixed-field read past buffer" {
try expectError(error.OffsetExceedsSize, deserialize(S, &buf, &out, std.testing.allocator));
}

test "deserialize: empty list of variable-size elements" {
const S = struct {
items: [][]const u8,
};
const empty: [][]const u8 = &.{};
const original = S{ .items = empty };

var list: ArrayList(u8) = .empty;
defer list.deinit(std.testing.allocator);
try serialize(S, original, &list, std.testing.allocator);

var out: S = undefined;
try deserialize(S, list.items, &out, std.testing.allocator);
defer std.testing.allocator.free(out.items);

try expect(out.items.len == 0);
}

test "deserialize struct: nested variable container truncated in outer slice" {
const Inner = struct {
x: u32,
Expand Down
Loading