[Mesa-dev] [PATCH v2] util: hash_table: move NULL assert to string hashing function

Lionel Landwerlin lionel.g.landwerlin at intel.com
Tue Nov 28 11:18:05 UTC 2017


Hash maps might use pointer keys (which people surely might want to
use to hash values) in which case a 0 value is perfectly acceptable.
It's only if the hash function needs to deference the pointer that we
want to be sure it's not NULL.

v2: Add assert in _mesa_fnv32_1a_accumulate_block (Ian)

Signed-off-by: Lionel Landwerlin <lionel.g.landwerlin at intel.com>
---
 src/util/hash_table.c | 4 ++--
 src/util/hash_table.h | 2 ++
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/src/util/hash_table.c b/src/util/hash_table.c
index b7421a0144c..8a4de565fcc 100644
--- a/src/util/hash_table.c
+++ b/src/util/hash_table.c
@@ -296,8 +296,6 @@ hash_table_insert(struct hash_table *ht, uint32_t hash,
    uint32_t start_hash_address, hash_address;
    struct hash_entry *available_entry = NULL;
 
-   assert(key != NULL);
-
    if (ht->entries >= ht->max_entries) {
       _mesa_hash_table_rehash(ht, ht->size_index + 1);
    } else if (ht->deleted_entries + ht->entries >= ht->max_entries) {
@@ -481,6 +479,8 @@ _mesa_hash_string(const void *_key)
    uint32_t hash = _mesa_fnv32_1a_offset_bias;
    const char *key = _key;
 
+   assert(key != NULL);
+
    while (*key != 0) {
       hash = _mesa_fnv32_1a_accumulate(hash, *key);
       key++;
diff --git a/src/util/hash_table.h b/src/util/hash_table.h
index d3e0758b265..34e9ea6aed3 100644
--- a/src/util/hash_table.h
+++ b/src/util/hash_table.h
@@ -118,6 +118,8 @@ _mesa_fnv32_1a_accumulate_block(uint32_t hash, const void *data, size_t size)
 {
    const uint8_t *bytes = (const uint8_t *)data;
 
+   assert(bytes != NULL);
+
    while (size-- != 0) {
       hash ^= *bytes;
       hash = hash * 0x01000193;
-- 
2.15.0



More information about the mesa-dev mailing list