From c878c48efbc5bedf3beaa7148d84acd31b50abe2 Mon Sep 17 00:00:00 2001 From: Beniamino Galvani Date: Wed, 18 Mar 2020 18:03:03 +0100 Subject: [PATCH] platform: sort IPv6 addresses from platform during sync There is no guarantee that addresses returned from the cache are in scope-priority order. Sort them. --- src/platform/nm-platform.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/platform/nm-platform.c b/src/platform/nm-platform.c index 699c8902f..c19a6e2da 100644 --- a/src/platform/nm-platform.c +++ b/src/platform/nm-platform.c @@ -3842,12 +3842,15 @@ ip6_address_scope_priority (const struct in6_addr *addr) } static int -ip6_address_scope_cmp (gconstpointer a, gconstpointer b) +ip6_address_scope_cmp (gconstpointer a, gconstpointer b, gpointer increasing) { const NMPlatformIP6Address *x = NMP_OBJECT_CAST_IP6_ADDRESS (*(const void **) a); const NMPlatformIP6Address *y = NMP_OBJECT_CAST_IP6_ADDRESS (*(const void **) b); + int ret; - return ip6_address_scope_priority (&x->address) - ip6_address_scope_priority (&y->address); + ret = ip6_address_scope_priority (&x->address) - ip6_address_scope_priority (&y->address); + + return GPOINTER_TO_INT (increasing) ? ret : -ret; } /** @@ -3888,7 +3891,7 @@ nm_platform_ip6_address_sync (NMPlatform *self, * apply the same sorting to known addresses, so that we don't try to * unnecessary change the order of addresses with different scopes. */ if (known_addresses) - g_ptr_array_sort (known_addresses, ip6_address_scope_cmp); + g_ptr_array_sort_with_data (known_addresses, ip6_address_scope_cmp, GINT_TO_POINTER (TRUE)); if (!_addr_array_clean_expired (AF_INET6, ifindex, known_addresses, now, &known_addresses_idx)) known_addresses = NULL; @@ -3904,6 +3907,8 @@ nm_platform_ip6_address_sync (NMPlatform *self, if (plat_addresses) { guint known_addresses_len; + g_ptr_array_sort_with_data (plat_addresses, ip6_address_scope_cmp, GINT_TO_POINTER (FALSE)); + known_addresses_len = known_addresses ? known_addresses->len : 0; /* First, compare every address whether it is still a "known address", that is, whether