authorgravatar for benjamin.feng@glassdoor.comBenjamin Feng <benjamin.feng@glassdoor.com> 2020-01-29 22:17:15-06:00
committergravatar for benjamin.feng@glassdoor.comBenjamin Feng <benjamin.feng@glassdoor.com> 2020-01-29 22:22:01-06:00
log5c8e85f3884c581b17c8df1f19852189fe7c9412
tree795cdc0de63d7d369633c35103f3fd794dc0eab6
parentb7a236d68e042ffcf2b0642e951ecca33ed842a4

Fix BufMap value leak


1 files changed, 4 insertions(+), 4 deletions(-)

lib/std/buf_map.zig+4-4
...@@ -43,9 +43,10 @@ pub const BufMap = struct {...@@ -43,9 +43,10 @@ pub const BufMap = struct {
43 pub fn set(self: *BufMap, key: []const u8, value: []const u8) !void {43 pub fn set(self: *BufMap, key: []const u8, value: []const u8) !void {
44 const value_copy = try self.copy(value);44 const value_copy = try self.copy(value);
45 errdefer self.free(value_copy);45 errdefer self.free(value_copy);
46 // Avoid copying key if it already exists
47 const get_or_put = try self.hash_map.getOrPut(key);46 const get_or_put = try self.hash_map.getOrPut(key);
48 if (!get_or_put.found_existing) {47 if (get_or_put.found_existing) {
48 self.free(get_or_put.kv.value);
49 } else {
49 get_or_put.kv.key = self.copy(key) catch |err| {50 get_or_put.kv.key = self.copy(key) catch |err| {
50 _ = self.hash_map.remove(key);51 _ = self.hash_map.remove(key);
51 return err;52 return err;
...@@ -83,8 +84,7 @@ pub const BufMap = struct {...@@ -83,8 +84,7 @@ pub const BufMap = struct {
83};84};
8485
85test "BufMap" {86test "BufMap" {
86 // TODO: uncomment and fix the leak87 var bufmap = BufMap.init(std.testing.allocator);
87 var bufmap = BufMap.init(std.heap.page_allocator);
88 defer bufmap.deinit();88 defer bufmap.deinit();
8989
90 try bufmap.set("x", "1");90 try bufmap.set("x", "1");