From 58fa2970687d75025c688c82c173c464eb480380 Mon Sep 17 00:00:00 2001 From: apio Date: Tue, 25 Jul 2023 18:19:45 +0200 Subject: [PATCH] libluna: Wrap around when iterating through a HashTable's buckets array Why am I so dumb? --- libluna/include/luna/HashTable.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/libluna/include/luna/HashTable.h b/libluna/include/luna/HashTable.h index 243be925..ddb724c7 100644 --- a/libluna/include/luna/HashTable.h +++ b/libluna/include/luna/HashTable.h @@ -21,6 +21,10 @@ template class HashTable u64 index = hash(value, m_salt) % m_capacity; + check(m_size < m_capacity); + + // NOTE: This should not end in an infinite loop if the table is full, since the table CANNOT be full + // (should_grow() returns true before it does). The check above verifies this. while (true) { auto& bucket = m_buckets[index]; @@ -28,6 +32,9 @@ template class HashTable { if (*bucket == value) return false; index++; + if (index == m_capacity) + index = 0; // Wrap around to avoid overflowing, seems like I assumed it would do that + // automatically for some reason... (facepalm). continue; } bucket = { move(value) }; @@ -53,6 +60,7 @@ template class HashTable { if (*bucket == value) return bucket.value_ptr(); i++; + if (i == m_capacity) i = 0; } else return nullptr; @@ -82,6 +90,7 @@ template class HashTable return true; } i++; + if (i == m_capacity) i = 0; } else return false;