Skip to content
/ zig Public
forked from ziglang/zig

Commit 149aa9a

Browse files
committed
add a workaround for miscompilation regarding alignment
See tracking issue ziglang#14904
1 parent bc79328 commit 149aa9a

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

lib/std/zig/Server.zig

+17-2
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,10 @@ pub fn receiveMessage(s: *Server) !InMessage.Header {
105105
assert(fifo.readableLength() == buf.len);
106106
if (buf.len >= @sizeOf(Header)) {
107107
const header = @ptrCast(*align(1) const Header, buf[0..@sizeOf(Header)]);
108-
const bytes_len = bswap(header.bytes_len);
109-
const tag = bswap(header.tag);
108+
// workaround for https://github.com/ziglang/zig/issues/14904
109+
const bytes_len = bswap_and_workaround_u32(&header.bytes_len);
110+
// workaround for https://github.com/ziglang/zig/issues/14904
111+
const tag = bswap_and_workaround_tag(&header.tag);
110112

111113
if (buf.len - @sizeOf(Header) >= bytes_len) {
112114
fifo.discard(@sizeOf(Header));
@@ -278,6 +280,19 @@ fn bswap_u32_array(slice: []u32) void {
278280
for (slice) |*elem| elem.* = @byteSwap(elem.*);
279281
}
280282

283+
/// workaround for https://github.com/ziglang/zig/issues/14904
284+
fn bswap_and_workaround_u32(x: *align(1) const u32) u32 {
285+
const bytes_ptr = @ptrCast(*const [4]u8, x);
286+
return std.mem.readIntLittle(u32, bytes_ptr);
287+
}
288+
289+
/// workaround for https://github.com/ziglang/zig/issues/14904
290+
fn bswap_and_workaround_tag(x: *align(1) const InMessage.Tag) InMessage.Tag {
291+
const bytes_ptr = @ptrCast(*const [4]u8, x);
292+
const int = std.mem.readIntLittle(u32, bytes_ptr);
293+
return @intToEnum(InMessage.Tag, int);
294+
}
295+
281296
const OutMessage = std.zig.Server.Message;
282297
const InMessage = std.zig.Client.Message;
283298

0 commit comments

Comments
 (0)