From 5b30656a50978880454261fcf122f3c4d4b872f7 Mon Sep 17 00:00:00 2001 From: Matthew Jakeman Date: Sat, 4 Oct 2025 01:25:37 +1000 Subject: [PATCH] fix: Guard against freeing invalid allocations --- src/memory.zig | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/memory.zig b/src/memory.zig index b72d580..d9042e4 100644 --- a/src/memory.zig +++ b/src/memory.zig @@ -108,9 +108,12 @@ fn zmeshFree(maybe_ptr: ?*anyopaque) callconv(.c) void { mem_mutex.lock(); defer mem_mutex.unlock(); - const size = mem_allocations.?.fetchRemove(@intFromPtr(ptr)).?.value; - const mem = @as([*]align(mem_alignment.toByteUnits()) u8, @ptrCast(@alignCast(ptr)))[0..size]; - mem_allocator.?.free(mem); + const try_get_allocation = mem_allocations.?.fetchRemove(@intFromPtr(ptr)); + if (try_get_allocation) |alloc| { + const size = alloc.value; + const mem = @as([*]align(mem_alignment.toByteUnits()) u8, @ptrCast(@alignCast(ptr)))[0..size]; + mem_allocator.?.free(mem); + } } }