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 6bc9b73c55)
This commit is contained in:
Thomas Haller 2022-03-28 21:19:21 +02:00
parent 403088a926
commit 3c692c0d00
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728

View file

@ -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;
}