Blob Blame History Raw
From adc7730a4e1b9721c93863a1b283457e9c02a3c5 Mon Sep 17 00:00:00 2001
From: Alexey Tikhonov <atikhono@redhat.com>
Date: Thu, 23 Jan 2020 17:55:24 +0100
Subject: [PATCH 18/23] sss_ptr_hash: don't keep empty sss_ptr_hash_delete_data
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

There is no need to allocate memory for `sss_ptr_hash_delete_data`
if table user doesn't provide custom delete callback.

Reviewed-by: Pavel Březina <pbrezina@redhat.com>
---
 src/util/sss_ptr_hash.c | 36 ++++++++++++++++++++----------------
 1 file changed, 20 insertions(+), 16 deletions(-)

diff --git a/src/util/sss_ptr_hash.c b/src/util/sss_ptr_hash.c
index 8f9762cb9..f8addec1e 100644
--- a/src/util/sss_ptr_hash.c
+++ b/src/util/sss_ptr_hash.c
@@ -138,12 +138,6 @@ sss_ptr_hash_delete_cb(hash_entry_t *item,
     struct sss_ptr_hash_value *value;
     struct hash_entry_t callback_entry;
 
-    data = talloc_get_type(pvt, struct sss_ptr_hash_delete_data);
-    if (data == NULL) {
-        DEBUG(SSSDBG_CRIT_FAILURE, "Invalid data!\n");
-        return;
-    }
-
     value = talloc_get_type(item->value.ptr, struct sss_ptr_hash_value);
     if (value == NULL) {
         DEBUG(SSSDBG_CRIT_FAILURE, "Invalid value!\n");
@@ -157,8 +151,14 @@ sss_ptr_hash_delete_cb(hash_entry_t *item,
     /* Free value, this also will disable spy */
     talloc_free(value);
 
-    /* Switch to the input value and call custom callback. */
-    if (data->callback != NULL) {
+    if (pvt != NULL) {
+        /* Switch to the input value and call custom callback. */
+        data = talloc_get_type(pvt, struct sss_ptr_hash_delete_data);
+        if (data == NULL) {
+            DEBUG(SSSDBG_CRIT_FAILURE, "Invalid data!\n");
+            return;
+        }
+
         data->callback(&callback_entry, deltype, data->pvt);
     }
 }
@@ -167,17 +167,19 @@ hash_table_t *sss_ptr_hash_create(TALLOC_CTX *mem_ctx,
                                   hash_delete_callback *del_cb,
                                   void *del_cb_pvt)
 {
-    struct sss_ptr_hash_delete_data *data;
+    struct sss_ptr_hash_delete_data *data = NULL;
     hash_table_t *table;
     errno_t ret;
 
-    data = talloc_zero(NULL, struct sss_ptr_hash_delete_data);
-    if (data == NULL) {
-        return NULL;
-    }
+    if (del_cb != NULL) {
+        data = talloc_zero(NULL, struct sss_ptr_hash_delete_data);
+        if (data == NULL) {
+            return NULL;
+        }
 
-    data->callback = del_cb;
-    data->pvt = del_cb_pvt;
+        data->callback = del_cb;
+        data->pvt = del_cb_pvt;
+    }
 
     ret = sss_hash_create_ex(mem_ctx, 10, &table, 0, 0, 0, 0,
                              sss_ptr_hash_delete_cb, data);
@@ -188,7 +190,9 @@ hash_table_t *sss_ptr_hash_create(TALLOC_CTX *mem_ctx,
         return NULL;
     }
 
-    talloc_steal(table, data);
+    if (data != NULL) {
+        talloc_steal(table, data);
+    }
 
     return table;
 }
-- 
2.20.1