mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
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:
@@ -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"});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user