std: fix memory leak in ArrayHashMap (#13001)

This commit is contained in:
GethDW 2022-10-04 03:57:53 +01:00 committed by GitHub
parent 8bbb022500
commit 9d5462dcb5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -773,9 +773,9 @@ pub fn ArrayHashMapUnmanaged(
}
}
try self.entries.ensureTotalCapacity(allocator, new_capacity);
const new_bit_index = try IndexHeader.findBitIndex(new_capacity);
const new_header = try IndexHeader.alloc(allocator, new_bit_index);
try self.entries.ensureTotalCapacity(allocator, new_capacity);
if (self.index_header) |old_header| old_header.free(allocator);
self.insertAllEntriesIntoNewHeader(if (store_hash) {} else ctx, new_header);
@ -2042,6 +2042,19 @@ test "ensure capacity" {
try testing.expect(initial_capacity == map.capacity());
}
test "ensure capacity leak" {
try testing.checkAllAllocationFailures(std.testing.allocator, struct {
pub fn f(allocator: Allocator) !void {
var map = AutoArrayHashMap(i32, i32).init(allocator);
defer map.deinit();
var i: i32 = 0;
// put more than `linear_scan_max` in so index_header gets allocated.
while (i <= 20) : (i += 1) try map.put(i, i);
}
}.f, .{});
}
test "big map" {
var map = AutoArrayHashMap(i32, i32).init(std.testing.allocator);
defer map.deinit();