summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2022-03-28 21:19:21 +0200
committerThomas Haller <thaller@redhat.com>2022-05-03 10:03:25 +0200
commit3c692c0d00499d92b29182c601dcfb11ca1cdcf5 (patch)
tree4d8e79f0530299f9b11207e50ed20e50efc767d8
parent403088a926831ea3a726c48fd0d3ee6850dff3ba (diff)
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). (cherry picked from commit 6bc9b73c5550b1dad722ef77952df187875ebbd9)
-rw-r--r--src/libnm-platform/nm-platform.c24
1 files changed, 10 insertions, 14 deletions
diff --git a/src/libnm-platform/nm-platform.c b/src/libnm-platform/nm-platform.c
index 785a5e2370..8033baa1ac 100644
--- a/src/libnm-platform/nm-platform.c
+++ b/src/libnm-platform/nm-platform.c
@@ -4432,7 +4432,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);
@@ -4442,8 +4442,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;
@@ -4453,13 +4451,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;
}
@@ -4470,7 +4467,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;
@@ -4502,8 +4499,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);
@@ -4641,13 +4636,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;
}