zig/test/behavior/bugs/13128.zig
Cody Tapscott e6ebdcb82e stage2 LLVM: Use a packed aggregate for union payload init
Without the packed qualifier, the type layout that we use to
initialize doesn't match the correct layout of the underlying
storage, causing corrupted data and past-the-end writes.
2022-10-11 15:42:01 -04:00

30 lines
847 B
Zig

const std = @import("std");
const builtin = @import("builtin");
const expect = std.testing.expect;
const U = union(enum) {
x: u128,
y: [17]u8,
};
fn foo(val: U) !void {
try expect(val.x == 1);
}
test "runtime union init, most-aligned field != largest" {
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
var x: u8 = 1;
try foo(.{ .x = x });
const val: U = @unionInit(U, "x", x);
try expect(val.x == 1);
const val2: U = .{ .x = x };
try expect(val2.x == 1);
}