copy out keys also in HashMap.copy_keys (#1569)

* copy out keys also in HashMap.copy_keys
* test for copying keys out
This commit is contained in:
Walther Chen
2024-10-25 19:57:00 -04:00
committed by GitHub
parent 8ce63106b9
commit fd1898b70a
3 changed files with 26 additions and 2 deletions

View File

@@ -28,4 +28,23 @@ fn void! copy_map() @test
y.free();
assert(alloc.allocated() == 0);
};
}
}
/*
Some Keys (including Strings) are deep_copied into the hashmap on insertion.
When copying keys out, the keys themselves must also be deep-copied out.
Otherwise when the map is freed, the copied-in keys will also be freed,
resulting in use-after-free.
*/
fn void! copy_keys() @test
{
String[] y;
@pool() {
IntMap x;
x.temp_init();
x.set("hello", 0); // keys copied into temp hashmap
y = x.copy_keys(allocator::heap()); // keys copied out
// end of pool: hashmap and its copied-in keys dropped
};
assert(y == {"hello"});
}