Skip to content

Commit

Permalink
add a workaround for miscompilation regarding alignment
Browse files Browse the repository at this point in the history
See tracking issue #14904
  • Loading branch information
andrewrk committed Mar 15, 2023
1 parent aa93931 commit c77f074
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions lib/std/zig/Server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit c77f074

Please sign in to comment.