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.
This commit is contained in:
Matthew Lugg 2025-11-17 03:29:57 +01:00 committed by Alex Rønne Petersen
parent 989e05d93b
commit cd7d8dff26
No known key found for this signature in database

View File

@ -238,26 +238,34 @@ pub fn serveErrorBundle(s: *Server, error_bundle: std.zig.ErrorBundle) !void {
try s.out.flush(); try s.out.flush();
} }
pub fn allocErrorBundle(allocator: std.mem.Allocator, body: []const u8) !std.zig.ErrorBundle { pub fn allocErrorBundle(gpa: std.mem.Allocator, body: []const u8) error{ OutOfMemory, EndOfStream }!std.zig.ErrorBundle {
const eb_hdr = @as(*align(1) const OutMessage.ErrorBundle, @ptrCast(body)); var r: Reader = .fixed(body);
const extra_bytes = const hdr = r.takeStruct(OutMessage.ErrorBundle, .little) catch |err| switch (err) {
body[@sizeOf(OutMessage.ErrorBundle)..][0 .. @sizeOf(u32) * eb_hdr.extra_len]; error.EndOfStream => |e| return e,
const string_bytes = error.ReadFailed => unreachable,
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 = .{ var eb: std.zig.ErrorBundle = .{
.string_bytes = &.{}, .string_bytes = &.{},
.extra = &.{}, .extra = &.{},
}; };
errdefer error_bundle.deinit(allocator); errdefer eb.deinit(gpa);
error_bundle.string_bytes = try allocator.dupe(u8, string_bytes); const extra = try gpa.alloc(u32, hdr.extra_len);
const extra = try allocator.alloc(u32, unaligned_extra.len); eb.extra = extra;
@memcpy(extra, unaligned_extra); const string_bytes = try gpa.alloc(u8, hdr.string_bytes_len);
error_bundle.extra = extra; 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 { pub const TestMetadata = struct {