authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-11-13 20:33:13+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-11-16 01:12:27+02:00
logb6b3462796fe38a071e6ed2cbcdbb5b024359f35
treea27dc4a4b577c70aa478dedc0704ef5404e4423a
parent11c64bfe6ec921a481b340392ab1d2377359586d

std.mem.Allocator: do not return undefined pointers from `create`

Closes #13517

2 files changed, 2 insertions(+), 2 deletions(-)

lib/std/mem/Allocator.zig+1-1
...@@ -167,7 +167,7 @@ pub inline fn rawFree(self: Allocator, buf: []u8, buf_align: u29, ret_addr: usiz...@@ -167,7 +167,7 @@ pub inline fn rawFree(self: Allocator, buf: []u8, buf_align: u29, ret_addr: usiz
167/// Returns a pointer to undefined memory.167/// Returns a pointer to undefined memory.
168/// Call `destroy` with the result to free the memory.168/// Call `destroy` with the result to free the memory.
169pub fn create(self: Allocator, comptime T: type) Error!*T {169pub fn create(self: Allocator, comptime T: type) Error!*T {
170 if (@sizeOf(T) == 0) return @as(*T, undefined);170 if (@sizeOf(T) == 0) return @intToPtr(*T, std.math.maxInt(usize));
171 const slice = try self.allocAdvancedWithRetAddr(T, null, 1, .exact, @returnAddress());171 const slice = try self.allocAdvancedWithRetAddr(T, null, 1, .exact, @returnAddress());
172 return &slice[0];172 return &slice[0];
173}173}
src/Sema.zig+1-1
...@@ -18754,7 +18754,7 @@ fn zirIntToPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -18754,7 +18754,7 @@ fn zirIntToPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
18754 const addr = val.toUnsignedInt(target);18754 const addr = val.toUnsignedInt(target);
18755 if (!ptr_ty.isAllowzeroPtr() and addr == 0)18755 if (!ptr_ty.isAllowzeroPtr() and addr == 0)
18756 return sema.fail(block, operand_src, "pointer type '{}' does not allow address zero", .{ptr_ty.fmt(sema.mod)});18756 return sema.fail(block, operand_src, "pointer type '{}' does not allow address zero", .{ptr_ty.fmt(sema.mod)});
18757 if (addr != 0 and addr % ptr_align != 0)18757 if (addr != 0 and ptr_align != 0 and addr % ptr_align != 0)
18758 return sema.fail(block, operand_src, "pointer type '{}' requires aligned address", .{ptr_ty.fmt(sema.mod)});18758 return sema.fail(block, operand_src, "pointer type '{}' requires aligned address", .{ptr_ty.fmt(sema.mod)});
1875918759
18760 const val_payload = try sema.arena.create(Value.Payload.U64);18760 const val_payload = try sema.arena.create(Value.Payload.U64);