From cb06a36a1888ea2b866618a7621d9d1211f1ec66 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Mon, 11 Dec 2017 11:37:21 +0100 Subject: [PATCH] libnm: avoid deep cloning list of address attribute names for nm_utils_ip_addresses_to_variant() It's only a temporary list. No need for cloning the names as well. --- libnm-core/nm-core-internal.h | 2 ++ libnm-core/nm-setting-ip-config.c | 12 ++++++++++-- libnm-core/nm-utils.c | 15 +++++++-------- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/libnm-core/nm-core-internal.h b/libnm-core/nm-core-internal.h index 15e5dc2a1..ba3abf36c 100644 --- a/libnm-core/nm-core-internal.h +++ b/libnm-core/nm-core-internal.h @@ -216,6 +216,8 @@ GHashTable *_nm_utils_copy_strdict (GHashTable *strdict); typedef gpointer (*NMUtilsCopyFunc) (gpointer); +const char **_nm_ip_address_get_attribute_names (const NMIPAddress *addr, gboolean sorted, guint *out_length); + gboolean _nm_ip_route_attribute_validate_all (const NMIPRoute *route); const char **_nm_ip_route_get_attribute_names (const NMIPRoute *route, gboolean sorted, guint *out_length); GHashTable *_nm_ip_route_get_attributes_direct (NMIPRoute *route); diff --git a/libnm-core/nm-setting-ip-config.c b/libnm-core/nm-setting-ip-config.c index b1b76256a..c65780444 100644 --- a/libnm-core/nm-setting-ip-config.c +++ b/libnm-core/nm-setting-ip-config.c @@ -526,6 +526,14 @@ nm_ip_address_set_prefix (NMIPAddress *address, address->prefix = prefix; } +const char ** +_nm_ip_address_get_attribute_names (const NMIPAddress *address, gboolean sorted, guint *out_length) +{ + nm_assert (address); + + return nm_utils_strdict_get_keys (address->attributes, sorted, out_length); +} + /** * nm_ip_address_get_attribute_names: * @address: the #NMIPAddress @@ -541,7 +549,7 @@ nm_ip_address_get_attribute_names (NMIPAddress *address) g_return_val_if_fail (address, NULL); - names = nm_utils_strdict_get_keys (address->attributes, TRUE, NULL); + names = _nm_ip_address_get_attribute_names (address, TRUE, NULL); return nm_utils_strv_make_deep_copied_nonnull (names); } @@ -1126,7 +1134,7 @@ _nm_ip_route_get_attributes_direct (NMIPRoute *route) const char ** _nm_ip_route_get_attribute_names (const NMIPRoute *route, gboolean sorted, guint *out_length) { - g_return_val_if_fail (route != NULL, NULL); + nm_assert (route); return nm_utils_strdict_get_keys (route->attributes, sorted, out_length); } diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c index a872d16cf..3dffc82a5 100644 --- a/libnm-core/nm-utils.c +++ b/libnm-core/nm-utils.c @@ -1878,7 +1878,7 @@ GVariant * nm_utils_ip_addresses_to_variant (GPtrArray *addresses) { GVariantBuilder builder; - int i; + guint i; g_variant_builder_init (&builder, G_VARIANT_TYPE ("aa{sv}")); @@ -1886,8 +1886,8 @@ nm_utils_ip_addresses_to_variant (GPtrArray *addresses) for (i = 0; i < addresses->len; i++) { NMIPAddress *addr = addresses->pdata[i]; GVariantBuilder addr_builder; - char **names; - int n; + gs_free const char **names = NULL; + guint j, len; g_variant_builder_init (&addr_builder, G_VARIANT_TYPE ("a{sv}")); g_variant_builder_add (&addr_builder, "{sv}", @@ -1897,13 +1897,12 @@ nm_utils_ip_addresses_to_variant (GPtrArray *addresses) "prefix", g_variant_new_uint32 (nm_ip_address_get_prefix (addr))); - names = nm_ip_address_get_attribute_names (addr); - for (n = 0; names[n]; n++) { + names = _nm_ip_address_get_attribute_names (addr, TRUE, &len); + for (j = 0; j < len; j++) { g_variant_builder_add (&addr_builder, "{sv}", - names[n], - nm_ip_address_get_attribute (addr, names[n])); + names[j], + nm_ip_address_get_attribute (addr, names[j])); } - g_strfreev (names); g_variant_builder_add (&builder, "a{sv}", &addr_builder); }