Blob Blame History Raw
From 79560617790781384dda2751701b523aed6b5cb4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
Date: Sat, 3 Feb 2018 10:58:06 +0100
Subject: [PATCH 65/90] sss_ptr_hash: add sss_ptr_get_value to make it useful
 in delete callbacks
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>

Reviewed-by: Tomáš Halman <thalman@redhat.com>
---
 src/util/sss_ptr_hash.c | 31 +++++++++++++++++++++++++++++++
 src/util/sss_ptr_hash.h | 15 +++++++++++++++
 2 files changed, 46 insertions(+)

diff --git a/src/util/sss_ptr_hash.c b/src/util/sss_ptr_hash.c
index 0f884c8b6..bc0db6f48 100644
--- a/src/util/sss_ptr_hash.c
+++ b/src/util/sss_ptr_hash.c
@@ -263,6 +263,12 @@ sss_ptr_hash_lookup_internal(hash_table_t *table,
         return NULL;
     }
 
+    /* This may happen if we are in delete callback
+     * and we try to search the hash table. */
+    if (table_value.ptr == NULL) {
+        return NULL;
+    }
+
     if (!sss_ptr_hash_check_type(table_value.ptr, "struct sss_ptr_hash_value")) {
         return NULL;
     }
@@ -288,6 +294,31 @@ void *_sss_ptr_hash_lookup(hash_table_t *table,
     return value->ptr;
 }
 
+void *_sss_ptr_get_value(hash_value_t *table_value,
+                         const char *type)
+{
+    struct sss_ptr_hash_value *value;
+
+    /* Check value type. */
+    if (table_value->type != HASH_VALUE_PTR) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "Invalid value type found: %d\n",
+              table_value->type);
+        return NULL;
+    }
+
+    if (!sss_ptr_hash_check_type(table_value->ptr, "struct sss_ptr_hash_value")) {
+        return NULL;
+    }
+
+    value = table_value->ptr;
+
+    if (!sss_ptr_hash_check_type(value->ptr, type)) {
+        return NULL;
+    }
+
+    return value->ptr;
+}
+
 void sss_ptr_hash_delete(hash_table_t *table,
                          const char *key,
                          bool free_value)
diff --git a/src/util/sss_ptr_hash.h b/src/util/sss_ptr_hash.h
index 510b9544f..56bb19a65 100644
--- a/src/util/sss_ptr_hash.h
+++ b/src/util/sss_ptr_hash.h
@@ -24,6 +24,8 @@
 #include <talloc.h>
 #include <dhash.h>
 
+#include "util/util.h"
+
 /**
  * Create a new hash table with string key and talloc pointer value with
  * possible delete callback.
@@ -91,6 +93,19 @@ void *_sss_ptr_hash_lookup(hash_table_t *table,
 #define sss_ptr_hash_lookup(table, key, type) \
     (type *)_sss_ptr_hash_lookup(table, key, #type)
 
+void *_sss_ptr_get_value(hash_value_t *table_value,
+                         const char *type);
+
+/**
+ * Obtain inserted talloc pointer from table value typed to @type.
+ * The type of the value must match with @type, otherwise NULL is returned.
+ *
+ * @return talloc_ptr If the value is found as type matches.
+ * @return NULL If the value is not found or if the type is invalid.
+ */
+#define sss_ptr_get_value(table_value, type) \
+    (type *)_sss_ptr_get_value(table_value, #type)
+
 /**
  * Delete @key from table. If @free_value is true then also the value
  * associated with @key is freed, otherwise it is left intact.
-- 
2.20.1