glib-aux: add and use nm_uuid_generate_from_strings_old()

For a long time we have a function like nm_uuid_generate_from_strings().
This was recently reworked and renamed, but it preserved behavior. Preserving
behavior is important for this function, because for the existing users,
we need to keep generating the same UUIDs.

Originally, this function was a variadic function with NULL sentinel.
That means, when you write

  nm_uuid_generate_from_strings(uuid_type, type_arg, v1, v2, v3, NULL);

and v2 happens to be NULL, then v3 is ignored. That is most likely not
what the user intended. Maybe they had a bug and v2 should not be NULL.
But nm_uuid_generate_from_strings() should not require that all
arguments are non-NULL and it should not ignore arguments after the
first NULL.

For example, one user works around this via

    uuid = nm_uuid_generate_from_strings_old("ibft",
                                             s_hwaddr,
                                             s_vlanid ? "V" : "v",
                                             s_vlanid ? s_vlanid : "",
                                             s_ipaddr ? "A" : "DHCP",
                                             s_ipaddr ? s_ipaddr : "");

which is cumbersome and ugly.

That will be fixed next, by adding a function that doesn't suffer
from this problem. But "this problem" is part of the API of the
function, we cannot just change it. Instead, rename it and all
users, so they can keep doing the same.

New users of course should no longer use the "old" function.
This commit is contained in:
Thomas Haller 2022-10-07 15:16:52 +02:00
parent a9bc3ec08b
commit b5e7e48bc1
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
6 changed files with 19 additions and 21 deletions

View file

@ -1725,12 +1725,10 @@ new_default_connection(NMDevice *self)
/* Create a stable UUID. The UUID is also the Network_ID for stable-privacy addr-gen-mode,
* thus when it changes we will also generate different IPv6 addresses. */
uuid = nm_uuid_generate_from_strings(NM_UUID_TYPE_VERSION3,
&nm_uuid_ns_1,
"default-wired",
nm_utils_machine_id_str(),
defname,
perm_hw_addr ?: iface);
uuid = nm_uuid_generate_from_strings_old("default-wired",
nm_utils_machine_id_str(),
defname,
perm_hw_addr ?: iface);
g_object_set(setting,
NM_SETTING_CONNECTION_ID,

View file

@ -2296,8 +2296,7 @@ test_read_missing_id_uuid(void)
gs_free char *expected_uuid = NULL;
const char *FILENAME = TEST_KEYFILES_DIR "/Test_Missing_ID_UUID";
expected_uuid =
nm_uuid_generate_from_strings(NM_UUID_TYPE_VERSION3, &nm_uuid_ns_1, "keyfile", FILENAME);
expected_uuid = nm_uuid_generate_from_strings_old("keyfile", FILENAME);
connection = keyfile_read_connection_from_file(FILENAME);

View file

@ -3800,10 +3800,7 @@ nm_keyfile_read_ensure_uuid(NMConnection *connection, const char *fallback_uuid_
if (nm_setting_connection_get_uuid(s_con))
return FALSE;
hashed_uuid = nm_uuid_generate_from_strings(NM_UUID_TYPE_VERSION3,
&nm_uuid_ns_1,
"keyfile",
fallback_uuid_seed);
hashed_uuid = nm_uuid_generate_from_strings_old("keyfile", fallback_uuid_seed);
g_object_set(s_con, NM_SETTING_CONNECTION_UUID, hashed_uuid, NULL);
return TRUE;
}

View file

@ -12,7 +12,7 @@
const NMUuid nm_uuid_ns_zero =
NM_UUID_INIT(00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00);
/* arbitrarily chosen namespace UUID for some uses of nm_uuid_generate_from_strings().
/* arbitrarily chosen namespace UUID for some uses of nm_uuid_generate_from_strings_old().
* Try not to re-use this namespace, instead, generate a unique one. */
const NMUuid nm_uuid_ns_1 =
NM_UUID_INIT(b4, 25, e9, fb, 75, 98, 44, b4, 9e, 3b, 5a, 2e, 3a, aa, 49, 05);

View file

@ -130,6 +130,12 @@ char *nm_uuid_generate_from_strings_strv(NMUuidType uuid_type,
#define nm_uuid_generate_from_strings(uuid_type, type_args, ...) \
nm_uuid_generate_from_strings_strv((uuid_type), (type_args), NM_MAKE_STRV(__VA_ARGS__))
/* Legacy function. Don't use for new code. */
#define nm_uuid_generate_from_strings_old(uuid_type, type_args, ...) \
nm_uuid_generate_from_strings_strv(NM_UUID_TYPE_VERSION3, \
&nm_uuid_ns_1, \
NM_MAKE_STRV(__VA_ARGS__))
/*****************************************************************************/
#endif /* __NM_UUID_H__ */

View file

@ -307,14 +307,12 @@ connection_setting_add(GHashTable *nic,
s_index ? " " : "",
s_index ? s_index : "");
uuid = nm_uuid_generate_from_strings(NM_UUID_TYPE_VERSION3,
&nm_uuid_ns_1,
"ibft",
s_hwaddr,
s_vlanid ? "V" : "v",
s_vlanid ? s_vlanid : "",
s_ipaddr ? "A" : "DHCP",
s_ipaddr ? s_ipaddr : "");
uuid = nm_uuid_generate_from_strings_old("ibft",
s_hwaddr,
s_vlanid ? "V" : "v",
s_vlanid ? s_vlanid : "",
s_ipaddr ? "A" : "DHCP",
s_ipaddr ? s_ipaddr : "");
s_con = (NMSetting *) nm_connection_get_setting_connection(connection);
if (!s_con) {