hash: fix false zero signature key hit lookup
authorPablo de Lara <pablo.de.lara.guarch@intel.com>
Fri, 26 Aug 2016 21:30:08 +0000 (22:30 +0100)
committerThomas Monjalon <thomas.monjalon@6wind.com>
Thu, 29 Sep 2016 19:50:32 +0000 (21:50 +0200)
This commit fixes a corner case scenario. When a key is deleted,
its signature in the hash table gets clear, which should prevent
a lookup of that same key, unless the signature of the key is all zeroes.

In that case, there will be a match, and key would be compared against
the key that is in the table (which does not get cleared,
as the performance penalty would be high), resulting in a wrong hit.

To prevent this from happening, the key index associated to that entry
should be set to zero when deleting it, so in case that same key
is looked up just after a deletion, it will point to the dummy key slot,
which guarantees a miss.

Fixes: 48a399119619 ("hash: replace with cuckoo hash implementation")

Signed-off-by: Pablo de Lara <pablo.de.lara.guarch@intel.com>
Acked-by: Saikrishna Edupuganti <saikrishna.edupuganti@intel.com>
lib/librte_hash/rte_cuckoo_hash.c

index 69fd663..32f9f11 100644 (file)
@@ -816,6 +816,7 @@ __rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key,
        unsigned i;
        struct rte_hash_bucket *bkt;
        struct rte_hash_key *k, *keys = h->key_store;
+       int32_t ret;
 
        bucket_idx = sig & h->bucket_bitmask;
        bkt = &h->buckets[bucket_idx];
@@ -833,7 +834,9 @@ __rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key,
                                 * Return index where key is stored,
                                 * substracting the first dummy index
                                 */
-                               return bkt->key_idx[i] - 1;
+                               ret = bkt->key_idx[i] - 1;
+                               bkt->key_idx[i] = 0;
+                               return ret;
                        }
                }
        }
@@ -856,7 +859,9 @@ __rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key,
                                 * Return index where key is stored,
                                 * substracting the first dummy index
                                 */
-                               return bkt->key_idx[i] - 1;
+                               ret = bkt->key_idx[i] - 1;
+                               bkt->key_idx[i] = 0;
+                               return ret;
                        }
                }
        }