summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2019-05-04 10:07:21 +0200
committerThomas Haller <thaller@redhat.com>2019-05-12 09:56:36 +0200
commit6c63799e4f2dc1336e6134d83c0d204c9b13c83c (patch)
tree8abe93443490797104bce770d5fd899d83c185cc
parent58ee66404d3c3fe626566b64b072ba140f3d0334 (diff)
downloadNetworkManager-6c63799e4f2dc1336e6134d83c0d204c9b13c83c.tar.gz
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).
-rw-r--r--src/nm-auth-utils.c30
1 files 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);
}