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

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