From 4edaf603c975ecc8f139e5c9e62b9a959a2fa26f Mon Sep 17 00:00:00 2001 From: Owen Shepherd Date: Fri, 16 Aug 2024 22:32:10 +0100 Subject: [PATCH] fix: Guard against uninitialized hashmap in key removal Removing non-present keys is a supported operation on HashMaps, and most other operations are well-defined on uninitialized HashMaps. Currently, removing any key on an uninitialized HashMap will result in an 'Array index out of bounds' error. This change guards against such a case. --- lib/std/collections/map.c3 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/std/collections/map.c3 b/lib/std/collections/map.c3 index fbb753131..9afa62e53 100644 --- a/lib/std/collections/map.c3 +++ b/lib/std/collections/map.c3 @@ -369,6 +369,7 @@ fn void HashMap.free_internal(&map, void* ptr) @inline @private fn bool HashMap.remove_entry_for_key(&map, Key key) @private { + if (!map.count) return false; uint hash = rehash(key.hash()); uint i = index_for(hash, map.table.len); Entry* prev = map.table[i]; @@ -421,4 +422,4 @@ struct Entry Key key; Value value; Entry* next; -} \ No newline at end of file +}