From b4b1daf0012c3007b762df31028fa35f5f68bd09 Mon Sep 17 00:00:00 2001 From: Kendall Condon <149842806+gooncreeper@users.noreply.github.com> Date: Wed, 26 Mar 2025 11:31:57 -0400 Subject: Allocator.create: properly handle alignment for zero-sized types (#21864) --- lib/std/heap.zig | 2 ++ lib/std/mem/Allocator.zig | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-) (limited to 'lib/std') diff --git a/lib/std/heap.zig b/lib/std/heap.zig index f0ea8d0d46..3e7647abb6 100644 --- a/lib/std/heap.zig +++ b/lib/std/heap.zig @@ -593,6 +593,8 @@ pub fn testAllocator(base_allocator: mem.Allocator) !void { const zero_bit_ptr = try allocator.create(u0); zero_bit_ptr.* = 0; allocator.destroy(zero_bit_ptr); + const zero_len_array = try allocator.create([0]u64); + allocator.destroy(zero_len_array); const oversize = try allocator.alignedAlloc(u32, null, 5); try testing.expect(oversize.len >= 5); diff --git a/lib/std/mem/Allocator.zig b/lib/std/mem/Allocator.zig index 9284e5ced3..c2f73096e8 100644 --- a/lib/std/mem/Allocator.zig +++ b/lib/std/mem/Allocator.zig @@ -150,7 +150,10 @@ pub inline fn rawFree(a: Allocator, memory: []u8, alignment: Alignment, ret_addr /// Returns a pointer to undefined memory. /// Call `destroy` with the result to free the memory. pub fn create(a: Allocator, comptime T: type) Error!*T { - if (@sizeOf(T) == 0) return @as(*T, @ptrFromInt(math.maxInt(usize))); + if (@sizeOf(T) == 0) { + const ptr = comptime std.mem.alignBackward(usize, math.maxInt(usize), @alignOf(T)); + return @as(*T, @ptrFromInt(ptr)); + } const ptr: *T = @ptrCast(try a.allocBytesWithAlignment(@alignOf(T), @sizeOf(T), @returnAddress())); return ptr; } -- cgit v1.2.3