From c1abbf40752027d5c0f8f4853312a882ae3088d6 Mon Sep 17 00:00:00 2001 From: Quinn Stephens Date: Mon, 9 Jun 2025 21:52:03 -0400 Subject: Fix hashmap_find() Modified hashmap_find() to properly fit the way lists work (where the list struct is technically a list entry at the end of a circular list). If this had not been fixed, hashmap_find() probably would have run into some sort of infinite loop or segmentation fault when it failed to find a matching entry. Signed-off-by: Quinn Stephens --- src/hashmap.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/hashmap.c b/src/hashmap.c index cc7f2db..d4d6358 100644 --- a/src/hashmap.c +++ b/src/hashmap.c @@ -46,16 +46,18 @@ hashmap_add(struct hashmap map, struct hashmap_entry *entry) struct hashmap_entry * hashmap_find(struct hashmap map, hash_t hash) { - struct hashmap_entry *head, *entry; + struct list *list; + struct hashmap_entry *entry; - entry = head = (struct hashmap_entry *)map.rows[hash % map.row_count].head; - do { + list = &map.rows[hash % map.row_count]; + entry = (struct hashmap_entry *)list->head; + while (entry != (struct hashmap_entry *)list) { if (entry->hash == hash) { return entry; } entry = (struct hashmap_entry *)entry->list_entry.next; - } while (entry != head); + } return NULL; } -- cgit v1.2.3