Skip to content

Commit 31e5445

Browse files
author
Uri Yagelnik
committed
hashtable: add bounds check in hashtableNext iterator
Prevent iteration beyond hashtable bounds by checking if iterator is already at the end before proceeding with the main iteration loop. This adds defensive bounds checking for table index and bucket index. Signed-off-by: Uri Yagelnik <uriy@amazon.com>
1 parent a47e8fa commit 31e5445

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

src/hashtable.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2025,6 +2025,14 @@ void hashtableReleaseIterator(hashtableIterator *iterator) {
20252025
* Returns false if there are no more entries. */
20262026
bool hashtableNext(hashtableIterator *iterator, void **elemptr) {
20272027
iter *iter = iteratorFromOpaque(iterator);
2028+
/* Check if iterator is already at the end */
2029+
if (iter->table > 1 ||
2030+
(iter->table == 1 && !hashtableIsRehashing(iter->hashtable)) ||
2031+
(iter->index >= 0 && iter->hashtable->tables[iter->table] &&
2032+
(size_t)iter->index >= numBuckets(iter->hashtable->bucket_exp[iter->table]))) {
2033+
return 0;
2034+
}
2035+
20282036
while (1) {
20292037
if (iter->index == -1 && iter->table == 0) {
20302038
/* It's the first call to next. */

0 commit comments

Comments
 (0)