]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Fix the assertion failure in the isc_hashmap iterator
authorOndřej Surý <ondrej@isc.org>
Mon, 12 Aug 2024 13:17:00 +0000 (15:17 +0200)
committerOndřej Surý <ondrej@isc.org>
Wed, 14 Aug 2024 15:19:04 +0000 (15:19 +0000)
When the round robin hashing reorders the map entries on deletion, we
were adjusting the iterator table size only when the reordering was
happening at the internal table boundary.  The iterator table size had
to be reduced by one to prevent seeing the entry that resized on
position [0] twice because it migrated to [iter->size - 1] position.

However, the same thing could happen when the same entry migrates a
second time from [iter->size - 1] to [iter->size - 2] position (and so
on) because the check that we are manipulating the entry just in the [0]
position was insufficient.  Instead of checking the position [pos == 0],
we now check that the [pos % iter->size == 0], thus ignoring all the
entries that might have moved back to the end of the internal table.

lib/isc/hashmap.c

index d0611b97d37fdf1f879cd9fd0725112ef37c8b5c..338e497079fc9467ef16e77696abaf95fffc8351 100644 (file)
@@ -310,7 +310,8 @@ isc_hashmap_find(const isc_hashmap_t *hashmap, const uint32_t hashval,
 
 static bool
 hashmap_delete_node(isc_hashmap_t *hashmap, hashmap_node_t *entry,
-                   uint32_t hashval, uint32_t psl, const uint8_t idx) {
+                   uint32_t hashval, uint32_t psl, const uint8_t idx,
+                   size_t size) {
        uint32_t pos;
        uint32_t hash;
        bool last = false;
@@ -318,7 +319,7 @@ hashmap_delete_node(isc_hashmap_t *hashmap, hashmap_node_t *entry,
        hashmap->count--;
 
        hash = isc_hash_bits32(hashval, hashmap->tables[idx].hashbits);
-       pos = hash + psl;
+       pos = (hash + psl) & hashmap->tables[idx].hashmask;
 
        while (true) {
                hashmap_node_t *node = NULL;
@@ -332,7 +333,7 @@ hashmap_delete_node(isc_hashmap_t *hashmap, hashmap_node_t *entry,
                        break;
                }
 
-               if (pos == 0) {
+               if ((pos % size) == 0) {
                        last = true;
                }
 
@@ -373,7 +374,7 @@ hashmap_rehash_one(isc_hashmap_t *hashmap) {
        node = oldtable[hashmap->hiter];
 
        (void)hashmap_delete_node(hashmap, &oldtable[hashmap->hiter],
-                                 node.hashval, node.psl, oldidx);
+                                 node.hashval, node.psl, oldidx, UINT32_MAX);
 
        isc_result_t result = hashmap_add(hashmap, node.hashval, NULL, node.key,
                                          node.value, NULL, hashmap->hindex);
@@ -470,7 +471,8 @@ isc_hashmap_delete(isc_hashmap_t *hashmap, const uint32_t hashval,
        node = hashmap_find(hashmap, hashval, match, key, &psl, &idx);
        if (node != NULL) {
                INSIST(node->key != NULL);
-               (void)hashmap_delete_node(hashmap, node, hashval, psl, idx);
+               (void)hashmap_delete_node(hashmap, node, hashval, psl, idx,
+                                         UINT32_MAX);
                result = ISC_R_SUCCESS;
        }
 
@@ -682,7 +684,7 @@ isc_hashmap_iter_delcurrent_next(isc_hashmap_iter_t *iter) {
                &iter->hashmap->tables[iter->hindex].table[iter->i];
 
        if (hashmap_delete_node(iter->hashmap, node, node->hashval, node->psl,
-                               iter->hindex))
+                               iter->hindex, iter->size))
        {
                /*
                 * We have seen the new last element so reduce the size