mirror of
https://codeberg.org/ziglang/zig.git
synced 2026-03-08 03:24:46 +01:00
14 lines
443 B
Zig
14 lines
443 B
Zig
const std = @import("std");
|
|
|
|
test "pointer alignment safety" {
|
|
var array align(4) = [_]u32{ 0x11111111, 0x11111111 };
|
|
const bytes = std.mem.sliceAsBytes(array[0..]);
|
|
try std.testing.expectEqual(0x11111111, foo(bytes));
|
|
}
|
|
fn foo(bytes: []u8) u32 {
|
|
const slice4 = bytes[1..5];
|
|
const int_slice = std.mem.bytesAsSlice(u32, @as([]align(4) u8, @alignCast(slice4)));
|
|
return int_slice[0];
|
|
}
|
|
|
|
// test_safety=incorrect alignment
|