From b6b3462796fe38a071e6ed2cbcdbb5b024359f35 Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Sun, 13 Nov 2022 20:33:13 +0200 Subject: [PATCH] std.mem.Allocator: do not return undefined pointers from `create` Closes #13517 --- lib/std/mem/Allocator.zig | 2 +- src/Sema.zig | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/std/mem/Allocator.zig b/lib/std/mem/Allocator.zig index 28ca194c29..92347c0919 100644 --- a/lib/std/mem/Allocator.zig +++ b/lib/std/mem/Allocator.zig @@ -167,7 +167,7 @@ pub inline fn rawFree(self: Allocator, buf: []u8, buf_align: u29, ret_addr: usiz /// Returns a pointer to undefined memory. /// Call `destroy` with the result to free the memory. pub fn create(self: Allocator, comptime T: type) Error!*T { - if (@sizeOf(T) == 0) return @as(*T, undefined); + if (@sizeOf(T) == 0) return @intToPtr(*T, std.math.maxInt(usize)); const slice = try self.allocAdvancedWithRetAddr(T, null, 1, .exact, @returnAddress()); return &slice[0]; } diff --git a/src/Sema.zig b/src/Sema.zig index 493202982e..0dc53c90fa 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -18754,7 +18754,7 @@ fn zirIntToPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai const addr = val.toUnsignedInt(target); if (!ptr_ty.isAllowzeroPtr() and addr == 0) return sema.fail(block, operand_src, "pointer type '{}' does not allow address zero", .{ptr_ty.fmt(sema.mod)}); - if (addr != 0 and addr % ptr_align != 0) + if (addr != 0 and ptr_align != 0 and addr % ptr_align != 0) return sema.fail(block, operand_src, "pointer type '{}' requires aligned address", .{ptr_ty.fmt(sema.mod)}); const val_payload = try sema.arena.create(Value.Payload.U64);