From 6c63799e4f2dc1336e6134d83c0d204c9b13c83c Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sat, 4 May 2019 10:07:21 +0200 Subject: auth-chain: don't allow setting the same user-data twice We track the user-data in a linked list. Hence, when setting a user data we would need to search the list whether the tag already exists. This has an overhead and makes set-data() O(n). But we really don't need this. The NMAuthChain allows a simple way to attach user-data to the request. We don't need a full-blown g_object_set_data() (which btw is also just implemented as a linked list). --- src/nm-auth-utils.c | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/src/nm-auth-utils.c b/src/nm-auth-utils.c index 368cd42194..b1b3d7042e 100644 --- a/src/nm-auth-utils.c +++ b/src/nm-auth-utils.c @@ -218,26 +218,24 @@ nm_auth_chain_set_data (NMAuthChain *self, * necessary, revert the code to use GHashTable again. */ nm_assert (c_list_length (&self->data_lst_head) < 25); - chain_data = _get_data (self, tag); - - if (data == NULL) { - if (chain_data) - chain_data_free (chain_data); - return; - } - - if (chain_data) { - gpointer old_data = chain_data->data; - GDestroyNotify old_destroy = chain_data->destroy; - - chain_data->data = data; - chain_data->destroy = data_destroy; - if (old_destroy) - old_destroy (old_data); + /* The tag must not yet exist. Otherwise we'd have to first search the linked + * list for an existing entry. */ + nm_assert (!_get_data (self, tag)); + + if (!data) { + /* we don't track user data of %NULL. + * + * In the past this had also the meaning of removing a user-data. But since + * nm_auth_chain_set_data() does not allow being called more than once + * for the same tag, we don't need to remove anything. */ return; } chain_data = chain_data_new_stale (tag, data, data_destroy); + + /* we assert that no duplicate tags are added. But still, add the new + * element to the front, so that it would shadow the duplicate element + * in the list. */ c_list_link_front (&self->data_lst_head, &chain_data->data_lst); } -- cgit v1.2.1