Skip to content

Commit 36ec42e

Browse files
authored
fix: Guard against freeing invalid allocations (#10)
1 parent cfcabe1 commit 36ec42e

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/memory.zig

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,12 @@ fn zmeshFree(maybe_ptr: ?*anyopaque) callconv(.c) void {
108108
mem_mutex.lock();
109109
defer mem_mutex.unlock();
110110

111-
const size = mem_allocations.?.fetchRemove(@intFromPtr(ptr)).?.value;
112-
const mem = @as([*]align(mem_alignment.toByteUnits()) u8, @ptrCast(@alignCast(ptr)))[0..size];
113-
mem_allocator.?.free(mem);
111+
const try_get_allocation = mem_allocations.?.fetchRemove(@intFromPtr(ptr));
112+
if (try_get_allocation) |alloc| {
113+
const size = alloc.value;
114+
const mem = @as([*]align(mem_alignment.toByteUnits()) u8, @ptrCast(@alignCast(ptr)))[0..size];
115+
mem_allocator.?.free(mem);
116+
}
114117
}
115118
}
116119

0 commit comments

Comments
 (0)