summaryrefslogtreecommitdiff
path: root/core/walk.zig
diff options
context:
space:
mode:
Diffstat (limited to 'core/walk.zig')
-rw-r--r--core/walk.zig58
1 files changed, 53 insertions, 5 deletions
diff --git a/core/walk.zig b/core/walk.zig
index d870052d..46bfadc5 100644
--- a/core/walk.zig
+++ b/core/walk.zig
@@ -457,8 +457,52 @@ pub const Reader = struct {
};
/// What an error read back is when this build has none of that name (an
-/// OS's own, a newer build's): the tape is another build's, and diverges.
-const TapeError = error{ EndOfTape, OutOfMemory, NoSuchError };
+/// OS's own, a newer build's): the tape is another build's, and diverges;
+/// BadTape, a value none of its type's (an enum's number of no name).
+const TapeError = error{ EndOfTape, OutOfMemory, NoSuchError, BadTape };
+
+/// An enum's bytes as `encode` writes them (`@sizeOf(E)` of them), read
+/// checked: null for a number none of `E`'s names has, which a tape or a
+/// dump read back may hold (cut, flipped, a hostile peer's), never cast.
+pub fn enumFrom(comptime E: type, bytes: *const [@sizeOf(E)]u8) ?E {
+ const info = @typeInfo(E).@"enum";
+ const wide = std.mem.bytesToValue(@Int(.unsigned, @sizeOf(E) * 8), bytes);
+ const tag: info.tag_type = @bitCast(@as(@Int(.unsigned, @bitSizeOf(info.tag_type)), @truncate(wide)));
+ if (info.mode == .nonexhaustive) return @fromBackingInt(tag);
+ return std.enums.fromInt(E, tag);
+}
+
+/// `bytes`, a run of `E`s copied whole, when each is one of `E`'s names (an
+/// enum's run is checked as one read alone is; another flat type's is bytes).
+fn named(comptime E: type, bytes: []const u8) error{BadTape}![]const u8 {
+ if (comptime @typeInfo(E) != .@"enum" or @typeInfo(E).@"enum".mode == .nonexhaustive) return bytes;
+ var at: usize = 0;
+ while (at < bytes.len) : (at += @sizeOf(E)) if (enumFrom(E, bytes[at..][0..@sizeOf(E)]) == null) return error.BadTape;
+ return bytes;
+}
+
+/// The fewest bytes `encode` writes for a `T`: a count read back is held to
+/// the bytes left before anything is made for it (0: no such bound).
+fn least(comptime T: type) usize {
+ return switch (@typeInfo(T)) {
+ .bool, .int, .float, .@"enum" => @sizeOf(T),
+ .optional, .error_union => 1,
+ .array => |a| a.len * least(a.child),
+ .pointer => |p| if (p.size == .slice) @sizeOf(usize) else 0,
+ .@"union" => |u| @sizeOf(u.tag_type.?),
+ .@"struct" => |s| sum: {
+ if (s.layout == .@"packed") break :sum @sizeOf(T);
+ if (machinery(T)) break :sum 0;
+ var n: usize = 0;
+ for (s.field_names, s.field_types, s.field_attrs) |name, F, attr| {
+ if (attr.@"comptime" or excluded(T, name)) continue;
+ n += least(F);
+ }
+ break :sum n;
+ },
+ else => 0,
+ };
+}
/// The inverse of `encode`; slices (and what a pointer points at) are made
/// in `a`. A field `encode` left out is its default.
@@ -466,7 +510,8 @@ pub fn decode(comptime T: type, r: *Reader, a: std.mem.Allocator) TapeError!T {
switch (@typeInfo(T)) {
.void, .null, .undefined => return {},
.bool => return (try r.take(1))[0] != 0,
- .int, .float, .@"enum" => return r.scalar(T),
+ .int, .float => return r.scalar(T),
+ .@"enum" => return enumFrom(T, (try r.take(@sizeOf(T)))[0..@sizeOf(T)]) orelse error.BadTape,
.optional => |o| {
if ((try r.take(1))[0] == 0) return null;
if (comptime machinery(o.child)) unreachable;
@@ -475,7 +520,7 @@ pub fn decode(comptime T: type, r: *Reader, a: std.mem.Allocator) TapeError!T {
.array => |a_info| {
var out: T = undefined;
if (comptime flat(a_info.child)) {
- @memcpy(std.mem.sliceAsBytes(&out), try r.take(@sizeOf(T)));
+ @memcpy(std.mem.sliceAsBytes(&out), try named(a_info.child, try r.take(@sizeOf(T))));
} else for (&out) |*e| {
e.* = try decode(a_info.child, r, a);
}
@@ -489,9 +534,12 @@ pub fn decode(comptime T: type, r: *Reader, a: std.mem.Allocator) TapeError!T {
},
.slice => {
const n = try r.scalar(usize);
+ // More than the bytes left could hold: refused before it is made.
+ const min = comptime least(p.child);
+ if (min > 0 and n > (r.bytes.len - r.at) / min) return error.EndOfTape;
const out = try a.alloc(p.child, n);
if (comptime flat(p.child)) {
- @memcpy(std.mem.sliceAsBytes(out), try r.take(n * @sizeOf(p.child)));
+ @memcpy(std.mem.sliceAsBytes(out), try named(p.child, try r.take(n * @sizeOf(p.child))));
} else for (out) |*e| {
e.* = try decode(p.child, r, a);
}