diff options
author | Thomas Haller <thaller@redhat.com> | 2019-05-06 10:04:16 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2019-05-12 09:56:36 +0200 |
commit | 5a3ffffe7453bf0fcde97e14f0cc7a2d85e8bce8 (patch) | |
tree | 177c13a22a668a2c81f74f31665125bbb3256522 /src/nm-auth-utils.c | |
parent | 04fafd582b87328be0087290feb2d9c9f59c8bbd (diff) | |
download | NetworkManager-5a3ffffe7453bf0fcde97e14f0cc7a2d85e8bce8.tar.gz |
auth-chain: don't copy ChainData tag
All callers pass a C string literal as the user-data tag of NMAuthChain.
It makes little sense otherwise because you usually know which user data
you need in advance.
So don't bother with copying the string. Just reference the defacto
static string.
Rename nm_auth_chain_set_data() to nm_auth_chain_set_data_unsafe() to indicate
that the lifetime of the tag string is now the caller's responsibility.
The nm_auth_chain_set_data() macro now ensures that the tag argument is
a C string literal. In fact, all callers that we had did that already.
Diffstat (limited to 'src/nm-auth-utils.c')
-rw-r--r-- | src/nm-auth-utils.c | 52 |
1 files changed, 26 insertions, 26 deletions
diff --git a/src/nm-auth-utils.c b/src/nm-auth-utils.c index 2e61ad2060..81ea66a2ab 100644 --- a/src/nm-auth-utils.c +++ b/src/nm-auth-utils.c @@ -115,37 +115,18 @@ _find_auth_call (NMAuthChain *self, const char *permission) typedef struct { CList data_lst; + const char *tag; gpointer data; GDestroyNotify destroy; - char tag[]; } ChainData; -static ChainData * -chain_data_new_stale (const char *tag, - gpointer data, - GDestroyNotify destroy) -{ - ChainData *chain_data; - gsize l_p_1; - - nm_assert (tag); - nm_assert (data); - - l_p_1 = strlen (tag) + 1; - chain_data = g_malloc (sizeof (ChainData) + l_p_1); - chain_data->data = data; - chain_data->destroy = destroy; - memcpy (&chain_data->tag[0], tag, l_p_1); - return chain_data; -} - static void chain_data_free (ChainData *chain_data) { c_list_unlink_stale (&chain_data->data_lst); if (chain_data->destroy) chain_data->destroy (chain_data->data); - g_free (chain_data); + g_slice_free (ChainData, chain_data); } static ChainData * @@ -204,11 +185,25 @@ nm_auth_chain_steal_data (NMAuthChain *self, const char *tag) return value; } +/** + * nm_auth_chain_set_data_unsafe: + * @self: the #NMAuthChain + * @tag: the tag for referencing the attached data. + * @data: the data to attach. If %NULL, this call has no effect + * and nothing is attached. + * @data_destroy: (allow-none): the destroy function for the data pointer. + * + * @tag string is not cloned and must outlife @self. That is why + * the function is "unsafe". Use nm_auth_chain_set_data() with a C literal + * instead. + * + * It is a bug to add the same tag more than once. + */ void -nm_auth_chain_set_data (NMAuthChain *self, - const char *tag, - gpointer data, - GDestroyNotify data_destroy) +nm_auth_chain_set_data_unsafe (NMAuthChain *self, + const char *tag, + gpointer data, + GDestroyNotify data_destroy) { ChainData *chain_data; @@ -232,7 +227,12 @@ nm_auth_chain_set_data (NMAuthChain *self, return; } - chain_data = chain_data_new_stale (tag, data, data_destroy); + chain_data = g_slice_new (ChainData); + *chain_data = (ChainData) { + .tag = tag, + .data = data, + .destroy = 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 |