summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2017-02-05 19:14:28 +0100
committerThomas Haller <thaller@redhat.com>2017-02-10 14:43:24 +0100
commita8221323990c80fa4ad585388b213788f12917c9 (patch)
tree617e0fdffb82a0c8ea227284483041a449aab995
parentb3b1793f3dc4fcedb63fe51ecc9d3ed17aea6d35 (diff)
downloadNetworkManager-a8221323990c80fa4ad585388b213788f12917c9.tar.gz
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.
-rw-r--r--src/nm-core-utils.c56
1 files changed, 43 insertions, 13 deletions
diff --git a/src/nm-core-utils.c b/src/nm-core-utils.c
index 29c26ba817..143fdfe8b9 100644
--- a/src/nm-core-utils.c
+++ b/src/nm-core-utils.c
@@ -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;
+
+ 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;
- 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;
+ 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;
}