zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

test_incorrect_pointer_alignment.zig (440B) - Raw


      1 const std = @import("std");
      2 
      3 test "pointer alignment safety" {
      4     var array align(4) = [_]u32{ 0x11111111, 0x11111111 };
      5     const bytes = std.mem.sliceAsBytes(array[0..]);
      6     try std.testing.expect(foo(bytes) == 0x11111111);
      7 }
      8 fn foo(bytes: []u8) u32 {
      9     const slice4 = bytes[1..5];
     10     const int_slice = std.mem.bytesAsSlice(u32, @as([]align(4) u8, @alignCast(slice4)));
     11     return int_slice[0];
     12 }
     13 
     14 // test_safety=incorrect alignment