mirror of
https://github.com/ziglang/zig.git
synced 2024-11-30 09:02:32 +00:00
Fix BufMap value leak
This commit is contained in:
parent
b7a236d68e
commit
5c8e85f388
@ -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");
|
||||
|
Loading…
Reference in New Issue
Block a user