authorgravatar for info@bnoordhuis.nlBen Noordhuis <info@bnoordhuis.nl> 2018-04-05 23:26:06+02:00
committergravatar for info@bnoordhuis.nlBen Noordhuis <info@bnoordhuis.nl> 2018-04-05 23:32:49+02:00
log9e8519b7a2c765b427f85f0aaa456256785eceb7
tree06843f758b7687d8e03e9dc0e4dec6a74957935b
parent8938429ea12ff2857ace5380932a7cd68d3b4ab1

fix use-after-free in BufMap.set()

closes #879

1 files changed, 28 insertions(+), 2 deletions(-)

std/buf_map.zig+28-2
...@@ -31,8 +31,8 @@ pub const BufMap = struct {...@@ -31,8 +31,8 @@ pub const BufMap = struct {
31 if (self.hash_map.get(key)) |entry| {31 if (self.hash_map.get(key)) |entry| {
32 const value_copy = try self.copy(value);32 const value_copy = try self.copy(value);
33 errdefer self.free(value_copy);33 errdefer self.free(value_copy);
34 _ = try self.hash_map.put(key, value_copy);34 const old_value = ??(try self.hash_map.put(key, value_copy));
35 self.free(entry.value);35 self.free(old_value);
36 } else {36 } else {
37 const key_copy = try self.copy(key);37 const key_copy = try self.copy(key);
38 errdefer self.free(key_copy);38 errdefer self.free(key_copy);
...@@ -71,3 +71,29 @@ pub const BufMap = struct {...@@ -71,3 +71,29 @@ pub const BufMap = struct {
71 return result;71 return result;
72 }72 }
73};73};
74
75const assert = @import("debug/index.zig").assert;
76const heap = @import("heap.zig");
77
78test "BufMap" {
79 var direct_allocator = heap.DirectAllocator.init();
80 defer direct_allocator.deinit();
81
82 var bufmap = BufMap.init(&direct_allocator.allocator);
83 defer bufmap.deinit();
84
85 try bufmap.set("x", "1");
86 assert(mem.eql(u8, ??bufmap.get("x"), "1"));
87 assert(1 == bufmap.count());
88
89 try bufmap.set("x", "2");
90 assert(mem.eql(u8, ??bufmap.get("x"), "2"));
91 assert(1 == bufmap.count());
92
93 try bufmap.set("x", "3");
94 assert(mem.eql(u8, ??bufmap.get("x"), "3"));
95 assert(1 == bufmap.count());
96
97 bufmap.delete("x");
98 assert(0 == bufmap.count());
99}