mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 13:54:21 +00:00
std.BufSet.clone: fix key ownership
This was introduced in d1a4654834: when a
BufSet clones the keys, it used to assign the new pointers to the old
struct. Fix that by assigning the pointers to the correct, i.e. the new,
struct.
This caused double-free when using arena allocator for the new struct,
also in the test case.
This commit is contained in:
parent
52205a3c16
commit
543bee0adf
1 changed files with 14 additions and 1 deletions
|
|
@ -78,7 +78,7 @@ pub const BufSet = struct {
|
||||||
) Allocator.Error!BufSet {
|
) Allocator.Error!BufSet {
|
||||||
var cloned_hashmap = try self.hash_map.cloneWithAllocator(new_allocator);
|
var cloned_hashmap = try self.hash_map.cloneWithAllocator(new_allocator);
|
||||||
var cloned = BufSet{ .hash_map = cloned_hashmap };
|
var cloned = BufSet{ .hash_map = cloned_hashmap };
|
||||||
var it = self.hash_map.keyIterator();
|
var it = cloned.hash_map.keyIterator();
|
||||||
while (it.next()) |key_ptr| {
|
while (it.next()) |key_ptr| {
|
||||||
key_ptr.* = try cloned.copy(key_ptr.*);
|
key_ptr.* = try cloned.copy(key_ptr.*);
|
||||||
}
|
}
|
||||||
|
|
@ -132,3 +132,16 @@ test "BufSet clone" {
|
||||||
original.cloneWithAllocator(testing.failing_allocator),
|
original.cloneWithAllocator(testing.failing_allocator),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test "BufSet.clone with arena" {
|
||||||
|
var allocator = std.testing.allocator;
|
||||||
|
var arena = std.heap.ArenaAllocator.init(allocator);
|
||||||
|
defer arena.deinit();
|
||||||
|
|
||||||
|
var buf = BufSet.init(allocator);
|
||||||
|
defer buf.deinit();
|
||||||
|
try buf.insert("member1");
|
||||||
|
try buf.insert("member2");
|
||||||
|
|
||||||
|
_ = try buf.cloneWithAllocator(arena.allocator());
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue