core: make nm_utils_cmp_connection_by_autoconnect_priority() more robust

Check for NULL and unexpected missing NMSettingConnection.
Be more forgiving and accept whatever is there when comparing
@a with @b.
This commit is contained in:
Thomas Haller 2017-02-05 19:14:28 +01:00
parent b3b1793f3d
commit a822132399

View file

@ -1975,27 +1975,57 @@ nm_utils_read_resolv_conf_dns_options (const char *rc_contents)
return options;
}
/*****************************************************************************/
/**
* nm_utils_cmp_connection_by_autoconnect_priority:
* @a:
* @b:
*
* compare connections @a and @b for their autoconnect property
* (with sorting the connection that has autoconnect enabled before
* the other)
* If they both have autoconnect enabled, sort them depending on their
* autoconnect-priority (with the higher priority first).
*
* If their autoconnect/autoconnect-priority is the same, 0 is returned.
* That is, they compare equal.
*
* Returns: -1, 0, or 1
*/
int
nm_utils_cmp_connection_by_autoconnect_priority (NMConnection *a, NMConnection *b)
{
NMSettingConnection *a_s_con, *b_s_con;
gboolean a_ac, b_ac;
gint a_ap, b_ap;
NMSettingConnection *a_s_con;
NMSettingConnection *b_s_con;
int a_ap, b_ap;
gboolean can_autoconnect;
if (a == b)
return 0;
if (!a)
return 1;
if (!b)
return -1;
a_s_con = nm_connection_get_setting_connection (a);
b_s_con = nm_connection_get_setting_connection (b);
a_ac = !!nm_setting_connection_get_autoconnect (a_s_con);
b_ac = !!nm_setting_connection_get_autoconnect (b_s_con);
if (a_ac != b_ac)
return ((int) b_ac) - ((int) a_ac);
if (!a_ac)
return 0;
if (!a_s_con)
return !b_s_con ? 0 : 1;
if (!b_s_con)
return -1;
a_ap = nm_setting_connection_get_autoconnect_priority (a_s_con);
b_ap = nm_setting_connection_get_autoconnect_priority (b_s_con);
if (a_ap != b_ap)
return (a_ap > b_ap) ? -1 : 1;
can_autoconnect = !!nm_setting_connection_get_autoconnect (a_s_con);
if (can_autoconnect != (!!nm_setting_connection_get_autoconnect (b_s_con)))
return can_autoconnect ? -1 : 1;
if (can_autoconnect) {
a_ap = nm_setting_connection_get_autoconnect_priority (a_s_con);
b_ap = nm_setting_connection_get_autoconnect_priority (b_s_con);
if (a_ap != b_ap)
return (a_ap > b_ap) ? -1 : 1;
}
return 0;
}