From c77f074fac9c571dac492865b278ddc7865fd4df Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 13 Mar 2023 14:35:59 -0700 Subject: [PATCH] add a workaround for miscompilation regarding alignment See tracking issue #14904 --- lib/std/zig/Server.zig | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/std/zig/Server.zig b/lib/std/zig/Server.zig index 011139f7c660..788e36178244 100644 --- a/lib/std/zig/Server.zig +++ b/lib/std/zig/Server.zig @@ -105,8 +105,10 @@ pub fn receiveMessage(s: *Server) !InMessage.Header { assert(fifo.readableLength() == buf.len); if (buf.len >= @sizeOf(Header)) { const header = @ptrCast(*align(1) const Header, buf[0..@sizeOf(Header)]); - const bytes_len = bswap(header.bytes_len); - const tag = bswap(header.tag); + // workaround for https://github.com/ziglang/zig/issues/14904 + const bytes_len = bswap_and_workaround_u32(&header.bytes_len); + // workaround for https://github.com/ziglang/zig/issues/14904 + const tag = bswap_and_workaround_tag(&header.tag); if (buf.len - @sizeOf(Header) >= bytes_len) { fifo.discard(@sizeOf(Header)); @@ -278,6 +280,19 @@ fn bswap_u32_array(slice: []u32) void { for (slice) |*elem| elem.* = @byteSwap(elem.*); } +/// workaround for https://github.com/ziglang/zig/issues/14904 +fn bswap_and_workaround_u32(x: *align(1) const u32) u32 { + const bytes_ptr = @ptrCast(*const [4]u8, x); + return std.mem.readIntLittle(u32, bytes_ptr); +} + +/// workaround for https://github.com/ziglang/zig/issues/14904 +fn bswap_and_workaround_tag(x: *align(1) const InMessage.Tag) InMessage.Tag { + const bytes_ptr = @ptrCast(*const [4]u8, x); + const int = std.mem.readIntLittle(u32, bytes_ptr); + return @intToEnum(InMessage.Tag, int); +} + const OutMessage = std.zig.Server.Message; const InMessage = std.zig.Client.Message;