authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-11-17 03:29:57+01:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-11-19 01:42:45+01:00
logcd7d8dff26280146a720d907755a41406de4d510
tree65acb1e7207c256c8690c79e48fdfac4ec8b7b9d
parent989e05d93b1d33724f751b871f0c95c464401d6f
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

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.

1 files changed, 23 insertions(+), 15 deletions(-)

lib/std/zig/Server.zig+23-15
......@@ -238,26 +238,34 @@ pub fn serveErrorBundle(s: *Server, error_bundle: std.zig.ErrorBundle) !void {
238238 try s.out.flush();
239239}
240240
241pub fn allocErrorBundle(allocator: std.mem.Allocator, body: []const u8) !std.zig.ErrorBundle {
242 const eb_hdr = @as(*align(1) const OutMessage.ErrorBundle, @ptrCast(body));
243 const extra_bytes =
244 body[@sizeOf(OutMessage.ErrorBundle)..][0 .. @sizeOf(u32) * eb_hdr.extra_len];
245 const string_bytes =
246 body[@sizeOf(OutMessage.ErrorBundle) + extra_bytes.len ..][0..eb_hdr.string_bytes_len];
247 const unaligned_extra: []align(1) const u32 = @ptrCast(extra_bytes);
248
249 var error_bundle: std.zig.ErrorBundle = .{
241pub fn allocErrorBundle(gpa: std.mem.Allocator, body: []const u8) error{ OutOfMemory, EndOfStream }!std.zig.ErrorBundle {
242 var r: Reader = .fixed(body);
243 const hdr = r.takeStruct(OutMessage.ErrorBundle, .little) catch |err| switch (err) {
244 error.EndOfStream => |e| return e,
245 error.ReadFailed => unreachable,
246 };
247
248 var eb: std.zig.ErrorBundle = .{
250249 .string_bytes = &.{},
251250 .extra = &.{},
252251 };
253 errdefer error_bundle.deinit(allocator);
252 errdefer eb.deinit(gpa);
253
254 const extra = try gpa.alloc(u32, hdr.extra_len);
255 eb.extra = extra;
256 const string_bytes = try gpa.alloc(u8, hdr.string_bytes_len);
257 eb.string_bytes = string_bytes;
254258
255 error_bundle.string_bytes = try allocator.dupe(u8, string_bytes);
256 const extra = try allocator.alloc(u32, unaligned_extra.len);
257 @memcpy(extra, unaligned_extra);
258 error_bundle.extra = extra;
259 r.readSliceEndian(u32, extra, .little) catch |err| switch (err) {
260 error.EndOfStream => |e| return e,
261 error.ReadFailed => unreachable,
262 };
263 r.readSliceAll(string_bytes) catch |err| switch (err) {
264 error.EndOfStream => |e| return e,
265 error.ReadFailed => unreachable,
266 };
259267
260 return error_bundle;
268 return eb;
261269}
262270
263271pub const TestMetadata = struct {