Mesa (master): util/set: Assert that keys are not reserved pointers

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Thu Jun 6 00:28:02 UTC 2019


Module: Mesa
Branch: master
Commit: 8306dabc03c9a030cacd078b56446e6548224a23
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=8306dabc03c9a030cacd078b56446e6548224a23

Author: Jason Ekstrand <jason at jlekstrand.net>
Date:   Wed Jun  5 16:56:20 2019 -0500

util/set: Assert that keys are not reserved pointers

If we insert a NULL key, it will appear to succeed but will mess up
entry counting.  Similar errors can occur if someone accidentally
inserts the deleted key.  The later is highly unlikely but technically
possible so we should guard against it too.

Reviewed-by: Kenneth Graunke <kenneth at whitecape.org>
Reviewed-by: Caio Marcelo de Oliveira Filho <caio.oliveira at intel.com>
Reviewed-by: Eric Anholt <eric at anholt.net>

---

 src/util/set.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/src/util/set.c b/src/util/set.c
index 4788b94324a..5173c24474c 100644
--- a/src/util/set.c
+++ b/src/util/set.c
@@ -92,6 +92,12 @@ static const struct {
    ENTRY(2147483648ul, 2362232233ul, 2362232231ul )
 };
 
+static inline bool
+key_pointer_is_reserved(const void *key)
+{
+   return key == NULL || key == deleted_key;
+}
+
 static int
 entry_is_free(struct set_entry *entry)
 {
@@ -214,6 +220,8 @@ _mesa_set_clear(struct set *set, void (*delete_function)(struct set_entry *entry
 static struct set_entry *
 set_search(const struct set *ht, uint32_t hash, const void *key)
 {
+   assert(!key_pointer_is_reserved(key));
+
    uint32_t size = ht->size;
    uint32_t start_address = util_fast_urem32(hash, size, ht->size_magic);
    uint32_t double_hash = util_fast_urem32(hash, ht->rehash,
@@ -337,6 +345,8 @@ set_search_or_add(struct set *ht, uint32_t hash, const void *key, bool *found)
 {
    struct set_entry *available_entry = NULL;
 
+   assert(!key_pointer_is_reserved(key));
+
    if (ht->entries >= ht->max_entries) {
       set_rehash(ht, ht->size_index + 1);
    } else if (ht->deleted_entries + ht->entries >= ht->max_entries) {




More information about the mesa-commit mailing list