authorgravatar for kenta@lithdew.netlithdew <kenta@lithdew.net> 2021-03-28 14:22:34+09:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-03-28 11:02:37+02:00
logc8d721aa429ae492a10bc3d21ed5487ad620a032
tree66a87b2fcbf18785b9283b979af6a7bc871db174
parent54b42a75960c81fc0f3ef83d7452a9f8483f53e2

array_hash_map: decrement entries slice len after popping from entries in pop() to prevent oob


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

lib/std/array_hash_map.zig+13-13
......@@ -687,8 +687,9 @@ pub fn ArrayHashMapUnmanaged(
687687
688688 /// Removes the last inserted `Entry` in the hash map and returns it.
689689 pub fn pop(self: *Self) Entry {
690 const top = self.entries.pop();
690 const top = self.entries.items[self.entries.items.len - 1];
691691 _ = self.removeWithHash(top.key, top.hash, .index_only);
692 self.entries.items.len -= 1;
692693 return top;
693694 }
694695
......@@ -1258,19 +1259,18 @@ test "pop" {
12581259 var map = AutoArrayHashMap(i32, i32).init(std.testing.allocator);
12591260 defer map.deinit();
12601261
1261 testing.expect((try map.fetchPut(1, 11)) == null);
1262 testing.expect((try map.fetchPut(2, 22)) == null);
1263 testing.expect((try map.fetchPut(3, 33)) == null);
1264 testing.expect((try map.fetchPut(4, 44)) == null);
1262 // Insert just enough entries so that the map expands. Afterwards,
1263 // pop all entries out of the map.
12651264
1266 const pop1 = map.pop();
1267 testing.expect(pop1.key == 4 and pop1.value == 44);
1268 const pop2 = map.pop();
1269 testing.expect(pop2.key == 3 and pop2.value == 33);
1270 const pop3 = map.pop();
1271 testing.expect(pop3.key == 2 and pop3.value == 22);
1272 const pop4 = map.pop();
1273 testing.expect(pop4.key == 1 and pop4.value == 11);
1265 var i: i32 = 0;
1266 while (i < 9) : (i += 1) {
1267 testing.expect((try map.fetchPut(i, i)) == null);
1268 }
1269
1270 while (i > 0) : (i -= 1) {
1271 const pop = map.pop();
1272 testing.expect(pop.key == i - 1 and pop.value == i - 1);
1273 }
12741274}
12751275
12761276test "reIndex" {