authorgravatar for 33738921+GethDW@users.noreply.github.comGethDW <33738921+GethDW@users.noreply.github.com> 2022-10-04 03:57:53+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-10-03 22:57:53-04:00
log9d5462dcb5b4b4601bdf2e628b9d80fb74000cb2
treec5815200652355beb2a4389bd01741e457372631
parent8bbb022500f0dca91e4e7c8e2dcffec1eb383a93
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

std: fix memory leak in ArrayHashMap (#13001)


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

lib/std/array_hash_map.zig+14-1
......@@ -773,9 +773,9 @@ pub fn ArrayHashMapUnmanaged(
773773 }
774774 }
775775
776 try self.entries.ensureTotalCapacity(allocator, new_capacity);
776777 const new_bit_index = try IndexHeader.findBitIndex(new_capacity);
777778 const new_header = try IndexHeader.alloc(allocator, new_bit_index);
778 try self.entries.ensureTotalCapacity(allocator, new_capacity);
779779
780780 if (self.index_header) |old_header| old_header.free(allocator);
781781 self.insertAllEntriesIntoNewHeader(if (store_hash) {} else ctx, new_header);
......@@ -2042,6 +2042,19 @@ test "ensure capacity" {
20422042 try testing.expect(initial_capacity == map.capacity());
20432043}
20442044
2045test "ensure capacity leak" {
2046 try testing.checkAllAllocationFailures(std.testing.allocator, struct {
2047 pub fn f(allocator: Allocator) !void {
2048 var map = AutoArrayHashMap(i32, i32).init(allocator);
2049 defer map.deinit();
2050
2051 var i: i32 = 0;
2052 // put more than `linear_scan_max` in so index_header gets allocated.
2053 while (i <= 20) : (i += 1) try map.put(i, i);
2054 }
2055 }.f, .{});
2056}
2057
20452058test "big map" {
20462059 var map = AutoArrayHashMap(i32, i32).init(std.testing.allocator);
20472060 defer map.deinit();