core: use NM_CMP_*() macro in route_compare()

nm_ip_route_get_prefix() and plen are guint type, hence the following
is not correct:

    plen = nm_ip_route_get_prefix (route1);
    r = plen - nm_ip_route_get_prefix (route2);
    if (r)
         return r > 0 ? 1 : -1;

Use the macro, it gets subtle cases like this right.

Fixes: b32bb36c61
This commit is contained in:
Thomas Haller 2019-02-08 13:07:56 +01:00
parent 167142c38e
commit 668dc1cd02

View file

@ -377,41 +377,36 @@ check_ip6_method (NMConnection *orig,
static int
route_compare (NMIPRoute *route1, NMIPRoute *route2, gint64 default_metric)
{
gint64 r, metric1, metric2;
int family;
guint plen;
NMIPAddr a1;
NMIPAddr a2;
guint64 m1;
guint64 m2;
int family;
guint plen;
family = nm_ip_route_get_family (route1);
r = family - nm_ip_route_get_family (route2);
if (r)
return r > 0 ? 1 : -1;
NM_CMP_DIRECT (family, nm_ip_route_get_family (route2));
nm_assert_addr_family (family);
plen = nm_ip_route_get_prefix (route1);
r = plen - nm_ip_route_get_prefix (route2);
if (r)
return r > 0 ? 1 : -1;
NM_CMP_DIRECT (plen, nm_ip_route_get_prefix (route2));
metric1 = nm_ip_route_get_metric (route1) == -1 ? default_metric : nm_ip_route_get_metric (route1);
metric2 = nm_ip_route_get_metric (route2) == -1 ? default_metric : nm_ip_route_get_metric (route2);
m1 = nm_ip_route_get_metric (route1);
m2 = nm_ip_route_get_metric (route2);
NM_CMP_DIRECT (m1 == -1 ? default_metric : m1,
m2 == -1 ? default_metric : m2);
r = metric1 - metric2;
if (r)
return r > 0 ? 1 : -1;
NM_CMP_DIRECT_STRCMP0 (nm_ip_route_get_next_hop (route1),
nm_ip_route_get_next_hop (route2));
r = g_strcmp0 (nm_ip_route_get_next_hop (route1), nm_ip_route_get_next_hop (route2));
if (r)
return r;
/* NMIPRoute validates family and dest. inet_pton() is not expected to fail. */
inet_pton (family, nm_ip_route_get_dest (route1), &a1);
inet_pton (family, nm_ip_route_get_dest (route2), &a2);
if (!inet_pton (family, nm_ip_route_get_dest (route1), &a1))
nm_assert_not_reached ();
if (!inet_pton (family, nm_ip_route_get_dest (route2), &a2))
nm_assert_not_reached ();
nm_utils_ipx_address_clear_host_address (family, &a1, &a1, plen);
nm_utils_ipx_address_clear_host_address (family, &a2, &a2, plen);
r = memcmp (&a1, &a2, nm_utils_addr_family_to_size (family));
if (r)
return r;
NM_CMP_DIRECT_MEMCMP (&a1, &a2, nm_utils_addr_family_to_size (family));
return 0;
}