commit cd7d8dff26280146a720d907755a41406de4d510 (tree)
parent 989e05d93b1d33724f751b871f0c95c464401d6f
Author: Matthew Lugg <mlugg@mlugg.co.uk>
Date: Mon, 17 Nov 2025 03:29:57 +0100
std.zig.Server: read error bundle as little-endian
Again, `std.zig.Server` expects little-endian. This is easy; we just use
a `Reader.fixed` instead of directly `@ptrCast`ing data out of the
buffer.
Diffstat:
1 file changed, 23 insertions(+), 15 deletions(-)
diff --git a/lib/std/zig/Server.zig b/lib/std/zig/Server.zig
@@ -238,26 +238,34 @@ pub fn serveErrorBundle(s: *Server, error_bundle: std.zig.ErrorBundle) !void {
try s.out.flush();
}
-pub fn allocErrorBundle(allocator: std.mem.Allocator, body: []const u8) !std.zig.ErrorBundle {
- const eb_hdr = @as(*align(1) const OutMessage.ErrorBundle, @ptrCast(body));
- const extra_bytes =
- body[@sizeOf(OutMessage.ErrorBundle)..][0 .. @sizeOf(u32) * eb_hdr.extra_len];
- const string_bytes =
- body[@sizeOf(OutMessage.ErrorBundle) + extra_bytes.len ..][0..eb_hdr.string_bytes_len];
- const unaligned_extra: []align(1) const u32 = @ptrCast(extra_bytes);
-
- var error_bundle: std.zig.ErrorBundle = .{
+pub fn allocErrorBundle(gpa: std.mem.Allocator, body: []const u8) error{ OutOfMemory, EndOfStream }!std.zig.ErrorBundle {
+ var r: Reader = .fixed(body);
+ const hdr = r.takeStruct(OutMessage.ErrorBundle, .little) catch |err| switch (err) {
+ error.EndOfStream => |e| return e,
+ error.ReadFailed => unreachable,
+ };
+
+ var eb: std.zig.ErrorBundle = .{
.string_bytes = &.{},
.extra = &.{},
};
- errdefer error_bundle.deinit(allocator);
+ errdefer eb.deinit(gpa);
+
+ const extra = try gpa.alloc(u32, hdr.extra_len);
+ eb.extra = extra;
+ const string_bytes = try gpa.alloc(u8, hdr.string_bytes_len);
+ eb.string_bytes = string_bytes;
- error_bundle.string_bytes = try allocator.dupe(u8, string_bytes);
- const extra = try allocator.alloc(u32, unaligned_extra.len);
- @memcpy(extra, unaligned_extra);
- error_bundle.extra = extra;
+ r.readSliceEndian(u32, extra, .little) catch |err| switch (err) {
+ error.EndOfStream => |e| return e,
+ error.ReadFailed => unreachable,
+ };
+ r.readSliceAll(string_bytes) catch |err| switch (err) {
+ error.EndOfStream => |e| return e,
+ error.ReadFailed => unreachable,
+ };
- return error_bundle;
+ return eb;
}
pub const TestMetadata = struct {