Blame SOURCES/0065-sss_ptr_hash-add-sss_ptr_get_value-to-make-it-useful.patch

5fca41
From 79560617790781384dda2751701b523aed6b5cb4 Mon Sep 17 00:00:00 2001
5fca41
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
5fca41
Date: Sat, 3 Feb 2018 10:58:06 +0100
5fca41
Subject: [PATCH 65/90] sss_ptr_hash: add sss_ptr_get_value to make it useful
5fca41
 in delete callbacks
5fca41
MIME-Version: 1.0
5fca41
Content-Type: text/plain; charset=UTF-8
5fca41
Content-Transfer-Encoding: 8bit
5fca41
5fca41
Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>
5fca41
5fca41
Reviewed-by: Tomáš Halman <thalman@redhat.com>
5fca41
---
5fca41
 src/util/sss_ptr_hash.c | 31 +++++++++++++++++++++++++++++++
5fca41
 src/util/sss_ptr_hash.h | 15 +++++++++++++++
5fca41
 2 files changed, 46 insertions(+)
5fca41
5fca41
diff --git a/src/util/sss_ptr_hash.c b/src/util/sss_ptr_hash.c
5fca41
index 0f884c8b6..bc0db6f48 100644
5fca41
--- a/src/util/sss_ptr_hash.c
5fca41
+++ b/src/util/sss_ptr_hash.c
5fca41
@@ -263,6 +263,12 @@ sss_ptr_hash_lookup_internal(hash_table_t *table,
5fca41
         return NULL;
5fca41
     }
5fca41
 
5fca41
+    /* This may happen if we are in delete callback
5fca41
+     * and we try to search the hash table. */
5fca41
+    if (table_value.ptr == NULL) {
5fca41
+        return NULL;
5fca41
+    }
5fca41
+
5fca41
     if (!sss_ptr_hash_check_type(table_value.ptr, "struct sss_ptr_hash_value")) {
5fca41
         return NULL;
5fca41
     }
5fca41
@@ -288,6 +294,31 @@ void *_sss_ptr_hash_lookup(hash_table_t *table,
5fca41
     return value->ptr;
5fca41
 }
5fca41
 
5fca41
+void *_sss_ptr_get_value(hash_value_t *table_value,
5fca41
+                         const char *type)
5fca41
+{
5fca41
+    struct sss_ptr_hash_value *value;
5fca41
+
5fca41
+    /* Check value type. */
5fca41
+    if (table_value->type != HASH_VALUE_PTR) {
5fca41
+        DEBUG(SSSDBG_CRIT_FAILURE, "Invalid value type found: %d\n",
5fca41
+              table_value->type);
5fca41
+        return NULL;
5fca41
+    }
5fca41
+
5fca41
+    if (!sss_ptr_hash_check_type(table_value->ptr, "struct sss_ptr_hash_value")) {
5fca41
+        return NULL;
5fca41
+    }
5fca41
+
5fca41
+    value = table_value->ptr;
5fca41
+
5fca41
+    if (!sss_ptr_hash_check_type(value->ptr, type)) {
5fca41
+        return NULL;
5fca41
+    }
5fca41
+
5fca41
+    return value->ptr;
5fca41
+}
5fca41
+
5fca41
 void sss_ptr_hash_delete(hash_table_t *table,
5fca41
                          const char *key,
5fca41
                          bool free_value)
5fca41
diff --git a/src/util/sss_ptr_hash.h b/src/util/sss_ptr_hash.h
5fca41
index 510b9544f..56bb19a65 100644
5fca41
--- a/src/util/sss_ptr_hash.h
5fca41
+++ b/src/util/sss_ptr_hash.h
5fca41
@@ -24,6 +24,8 @@
5fca41
 #include <talloc.h>
5fca41
 #include <dhash.h>
5fca41
 
5fca41
+#include "util/util.h"
5fca41
+
5fca41
 /**
5fca41
  * Create a new hash table with string key and talloc pointer value with
5fca41
  * possible delete callback.
5fca41
@@ -91,6 +93,19 @@ void *_sss_ptr_hash_lookup(hash_table_t *table,
5fca41
 #define sss_ptr_hash_lookup(table, key, type) \
5fca41
     (type *)_sss_ptr_hash_lookup(table, key, #type)
5fca41
 
5fca41
+void *_sss_ptr_get_value(hash_value_t *table_value,
5fca41
+                         const char *type);
5fca41
+
5fca41
+/**
5fca41
+ * Obtain inserted talloc pointer from table value typed to @type.
5fca41
+ * The type of the value must match with @type, otherwise NULL is returned.
5fca41
+ *
5fca41
+ * @return talloc_ptr If the value is found as type matches.
5fca41
+ * @return NULL If the value is not found or if the type is invalid.
5fca41
+ */
5fca41
+#define sss_ptr_get_value(table_value, type) \
5fca41
+    (type *)_sss_ptr_get_value(table_value, #type)
5fca41
+
5fca41
 /**
5fca41
  * Delete @key from table. If @free_value is true then also the value
5fca41
  * associated with @key is freed, otherwise it is left intact.
5fca41
-- 
5fca41
2.20.1
5fca41