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

Closes #13517
This commit is contained in:
Veikka Tuominen 2022-11-13 20:33:13 +02:00
parent 11c64bfe6e
commit b6b3462796
2 changed files with 2 additions and 2 deletions

View File

@ -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];
}

View File

@ -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);