From 07d728fbe60fa425960168b25d56f0f7f82d6c02 Mon Sep 17 00:00:00 2001 From: BlockOG <68442822+BlockOG@users.noreply.github.com> Date: Wed, 26 Feb 2025 23:16:54 +0200 Subject: [PATCH] std.mem.Allocator.free: fix freeing of single pointer to array with sentinel --- lib/std/mem/Allocator.zig | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/std/mem/Allocator.zig b/lib/std/mem/Allocator.zig index 1ad953311604..fa18693f05d1 100644 --- a/lib/std/mem/Allocator.zig +++ b/lib/std/mem/Allocator.zig @@ -398,8 +398,17 @@ pub fn reallocAdvanced( /// To free a single item, see `destroy`. pub fn free(self: Allocator, memory: anytype) void { const Slice = @typeInfo(@TypeOf(memory)).pointer; + const SliceChild = @typeInfo(Slice.child); const bytes = mem.sliceAsBytes(memory); - const bytes_len = bytes.len + if (Slice.sentinel() != null) @sizeOf(Slice.child) else 0; + const bytes_len = bytes.len + + if (Slice.sentinel() != null) + @sizeOf(Slice.child) + else if (Slice.size == .one and + SliceChild == .array and + SliceChild.array.sentinel() != null) + @sizeOf(SliceChild.array.child) + else + 0; if (bytes_len == 0) return; const non_const_ptr = @constCast(bytes.ptr); @memset(non_const_ptr[0..bytes_len], undefined);