From 6bc9b73c5550b1dad722ef77952df187875ebbd9 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Mon, 28 Mar 2022 21:19:21 +0200 Subject: [PATCH] platform: allocate result array when needed in nm_platform_ip_{address,route}_get_prune_list() It is rather unlikely, that we call this function with no existing routes/addresses. Hence, usually this does not safe an allocation of the GPtrArray. However, it's slightly less code and makes more sense this way (instead of checking afterwards, whether the array is empty and destroy it). --- src/libnm-platform/nm-platform.c | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/libnm-platform/nm-platform.c b/src/libnm-platform/nm-platform.c index 85ccde782c..6b1b395cbc 100644 --- a/src/libnm-platform/nm-platform.c +++ b/src/libnm-platform/nm-platform.c @@ -4431,7 +4431,7 @@ nm_platform_ip_address_get_prune_list(NMPlatform *self, const int IS_IPv4 = NM_IS_IPv4(addr_family); const NMDedupMultiHeadEntry *head_entry; NMPLookup lookup; - GPtrArray *result; + GPtrArray *result = NULL; CList *iter; nmp_lookup_init_object(&lookup, NMP_OBJECT_TYPE_IP_ADDRESS(NM_IS_IPv4(addr_family)), ifindex); @@ -4441,8 +4441,6 @@ nm_platform_ip_address_get_prune_list(NMPlatform *self, if (!head_entry) return NULL; - result = g_ptr_array_new_full(head_entry->len, (GDestroyNotify) nmp_object_unref); - c_list_for_each (iter, &head_entry->lst_entries_head) { const NMPObject *obj = c_list_entry(iter, NMDedupMultiEntry, lst_entries)->obj; @@ -4452,13 +4450,12 @@ nm_platform_ip_address_get_prune_list(NMPlatform *self, continue; } + if (!result) + result = g_ptr_array_new_full(head_entry->len, (GDestroyNotify) nmp_object_unref); + g_ptr_array_add(result, (gpointer) nmp_object_ref(obj)); } - if (result->len == 0) { - g_ptr_array_unref(result); - return NULL; - } return result; } @@ -4469,7 +4466,7 @@ nm_platform_ip_route_get_prune_list(NMPlatform *self, NMIPRouteTableSyncMode route_table_sync) { NMPLookup lookup; - GPtrArray *routes_prune; + GPtrArray *routes_prune = NULL; const NMDedupMultiHeadEntry *head_entry; CList *iter; NMPlatformIP4Route rt_local4; @@ -4501,8 +4498,6 @@ nm_platform_ip_route_get_prune_list(NMPlatform *self, rt_local6.plen = 0; rt_mcast6.plen = 0; - routes_prune = g_ptr_array_new_full(head_entry->len, (GDestroyNotify) nm_dedup_multi_obj_unref); - c_list_for_each (iter, &head_entry->lst_entries_head) { const NMPObject *obj = c_list_entry(iter, NMDedupMultiEntry, lst_entries)->obj; const NMPlatformIPXRoute *rt = NMP_OBJECT_CAST_IPX_ROUTE(obj); @@ -4640,13 +4635,14 @@ nm_platform_ip_route_get_prune_list(NMPlatform *self, break; } + if (!routes_prune) { + routes_prune = + g_ptr_array_new_full(head_entry->len, (GDestroyNotify) nm_dedup_multi_obj_unref); + } + g_ptr_array_add(routes_prune, (gpointer) nmp_object_ref(obj)); } - if (routes_prune->len == 0) { - g_ptr_array_unref(routes_prune); - return NULL; - } return routes_prune; }