diff options
Diffstat (limited to 'core')
| -rw-r--r-- | core/fuzz.zig | 4 | ||||
| -rw-r--r-- | core/walk.zig | 44 |
2 files changed, 44 insertions, 4 deletions
diff --git a/core/fuzz.zig b/core/fuzz.zig index 2d589054..091eca63 100644 --- a/core/fuzz.zig +++ b/core/fuzz.zig @@ -812,6 +812,10 @@ test "a dump and an image cut, flipped and grown: refused or loaded, never a pan // Join seed 31554: the namespace's opens_len past its 64 slots, refused, // and the rollback's teardown of the namespace read that far. try loadSeed(bytes, .join, 31554); + // Join seed 6356: the messages' `live_len` read back without their + // `live` (the core read in place, its own `live` undefined), a live + // message's `src` length 0xAAAA when the digest walked it. + try loadSeed(bytes, .join, 6356); for ([_][]const u8{ bytes, image }, [_]Road{ .join, .restore }, [_]u64{ 2048, 1024 }) |whole, road, n| { try loadSeed(whole, road, 0); for (1..n + 1) |seed| try loadSeed(whole, road, seed); diff --git a/core/walk.zig b/core/walk.zig index 6734c0b7..309842f1 100644 --- a/core/walk.zig +++ b/core/walk.zig @@ -236,6 +236,17 @@ fn pairAt(comptime T: type, comptime name: []const u8) ?usize { return null; } +/// How far each of `out`'s arrays a length cuts holds before a read: +/// nothing for a new value; for one read in place (`existing`), as far as +/// its length says now. +fn heldNow(comptime T: type, out: *const T, existing: bool) [pairs(T)]usize { + var held: [pairs(T)]usize = @splat(0); + if (existing) inline for (0..comptime pairs(T)) |i| { + held[i] = heldOf(T, out, T.replica.len[i][0]); + }; + return held; +} + /// A length that says more than its buffer holds. const Past = struct { name: []const u8, says: usize, holds: usize }; @@ -1904,9 +1915,11 @@ pub const Loader = struct { const outer = l.field; defer l.field = outer; const dropped = l.dropped; - // How far each array a length cuts was read: none yet (one read in - // place holds what it held). - var held: [pairs(T)]usize = @splat(if (existing) std.math.maxInt(usize) else 0); + // How far each array a length cuts was read: none yet, or for one + // read in place as far as its length said before the read (a fresh + // core's `undefined` past it), so a length read back without its + // array holds no more than that. + var held = heldNow(T, out, existing); if (l.same) { if (s.fields.len != names.len) return error.BadDump; inline for (names, 0..) |field, k| { @@ -2819,7 +2832,7 @@ pub const Get = struct { var found: std.StaticBitSet(@max(1, names.len)) = .empty; var failed = false; // How far each array a length cuts was read (Load's `fields`). - var held: [pairs(T)]usize = @splat(if (existing) std.math.maxInt(usize) else 0); + var held = heldNow(T, out, existing); var it = map.iterator() catch return error.BadDump; while (it.next() catch return error.BadDump) |kv_at| { const kv = kv_at.readKeyValuePair() catch return error.BadDump; @@ -3038,6 +3051,29 @@ test "a dump comes back whole: shared things shared, lists and runs as long as t try std.testing.expectEqualSlices(u8, bytes, again); } +test "a struct read in place holds a length read back without its array to what the array held: past it, the dump is refused" { + const gpa = std.testing.allocator; + const Old = struct { n: u8 = 0 }; + const New = struct { + pub const replica = .{ .len = .{.{ "buf", "n" }} }; + buf: [4]u32 = undefined, + n: u8 = 0, + }; + const bytes = try save(Old, gpa, &.{ .n = 3 }); + defer gpa.free(bytes); + // Holding none: the 3 read back name what was never written. + var l = try Load(&.{New}).init(gpa, bytes); + defer l.deinit(); + var out: New = .{}; + try std.testing.expectError(error.BadDump, l.into(New, &out)); + // Holding 3 as it stood: they stand. + var again = try Load(&.{New}).init(gpa, bytes); + defer again.deinit(); + var held: New = .{ .buf = .{ 7, 8, 9, 0 }, .n = 3 }; + try again.into(New, &held); + try std.testing.expectEqualSlices(u32, &.{ 7, 8, 9 }, held.buf[0..held.n]); +} + test "a dump crosses builds: a field added takes its default, one removed is passed over, one changed is left out" { const gpa = std.testing.allocator; const Old = struct { |
