From cd7d8dff26280146a720d907755a41406de4d510 Mon Sep 17 00:00:00 2001 From: Matthew Lugg Date: Mon, 17 Nov 2025 03:29:57 +0100 Subject: [PATCH] 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. --- lib/std/zig/Server.zig | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/lib/std/zig/Server.zig b/lib/std/zig/Server.zig index ffc596e8dc..a1166f7aa7 100644 --- 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); +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 error_bundle: std.zig.ErrorBundle = .{ + var eb: std.zig.ErrorBundle = .{ .string_bytes = &.{}, .extra = &.{}, }; - errdefer error_bundle.deinit(allocator); + errdefer eb.deinit(gpa); - 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; + 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; - return error_bundle; + 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 eb; } pub const TestMetadata = struct {