diff --git a/lib/std/buf_map.zig b/lib/std/buf_map.zig index df90025bae..e8bc735b57 100644 --- a/lib/std/buf_map.zig +++ b/lib/std/buf_map.zig @@ -43,9 +43,10 @@ pub const BufMap = struct { pub fn set(self: *BufMap, key: []const u8, value: []const u8) !void { const value_copy = try self.copy(value); errdefer self.free(value_copy); - // Avoid copying key if it already exists const get_or_put = try self.hash_map.getOrPut(key); - if (!get_or_put.found_existing) { + if (get_or_put.found_existing) { + self.free(get_or_put.kv.value); + } else { get_or_put.kv.key = self.copy(key) catch |err| { _ = self.hash_map.remove(key); return err; @@ -83,8 +84,7 @@ pub const BufMap = struct { }; test "BufMap" { - // TODO: uncomment and fix the leak - var bufmap = BufMap.init(std.heap.page_allocator); + var bufmap = BufMap.init(std.testing.allocator); defer bufmap.deinit(); try bufmap.set("x", "1");