Skip to content

Commit 6df1a9a

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 6df1a9a

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

src/hashtable.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ void hashtableSetResizePolicy(hashtableResizePolicy policy) {
148148
#define ENTRIES_PER_BUCKET 7
149149
#define BUCKET_BITS_TYPE uint8_t
150150
#define BITS_NEEDED_TO_STORE_POS_WITHIN_BUCKET 3
151+
/* Iterator table value indicating iteration is complete */
152+
#define HASHTABLE_ITER_END_TABLE 2
151153

152154
/* Selecting the number of buckets.
153155
*
@@ -2025,6 +2027,12 @@ void hashtableReleaseIterator(hashtableIterator *iterator) {
20252027
* Returns false if there are no more entries. */
20262028
bool hashtableNext(hashtableIterator *iterator, void **elemptr) {
20272029
iter *iter = iteratorFromOpaque(iterator);
2030+
2031+
assert(iter->table <= HASHTABLE_ITER_END_TABLE);
2032+
if (iter->table == HASHTABLE_ITER_END_TABLE) {
2033+
return false;
2034+
}
2035+
20282036
while (1) {
20292037
if (iter->index == -1 && iter->table == 0) {
20302038
/* It's the first call to next. */
@@ -2102,6 +2110,7 @@ bool hashtableNext(hashtableIterator *iterator, void **elemptr) {
21022110
}
21032111
return true;
21042112
}
2113+
iter->table = HASHTABLE_ITER_END_TABLE;
21052114
return false;
21062115
}
21072116

0 commit comments

Comments
 (0)