commit 5c8e85f3884c581b17c8df1f19852189fe7c9412 (tree)
parent b7a236d68e042ffcf2b0642e951ecca33ed842a4
Author: Benjamin Feng <benjamin.feng@glassdoor.com>
Date: Wed, 29 Jan 2020 22:17:15 -0600
Fix BufMap value leak
Diffstat:
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git 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");