From a7249cc619852eb6016eca367d8662f5ccb5f772 Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Thu, 16 Jan 2014 14:14:42 -0500 Subject: [PATCH] core: fix a possible crash if given an empty IP4 config update_system_hostname() was bailing out if (there is no IP4 config or the IP4 config has no addresses) AND (there is no IP6 config or the IP6 config has no addresses), but it would then hit an assertion and crash if there was a valid IP6 config along with an IP4 config with no addresses. Fix that and get rid of some redundancy. Sort of pointed out by Coverity. --- src/nm-policy.c | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/src/nm-policy.c b/src/nm-policy.c index 251bd79cd0..34f194fade 100644 --- a/src/nm-policy.c +++ b/src/nm-policy.c @@ -464,32 +464,20 @@ update_system_hostname (NMPolicy *policy, NMDevice *best4, NMDevice *best6) ip4_config = best4 ? nm_device_get_ip4_config (best4) : NULL; ip6_config = best6 ? nm_device_get_ip6_config (best6) : NULL; - if ( (!ip4_config || (nm_ip4_config_get_num_addresses (ip4_config) == 0)) - && (!ip6_config || (nm_ip6_config_get_num_addresses (ip6_config) == 0))) { - /* No valid IP config; fall back to localhost.localdomain */ - _set_hostname (policy, NULL, "no IP config"); - return; - } - - if (ip4_config) { + if (ip4_config && nm_ip4_config_get_num_addresses (ip4_config) > 0) { const NMPlatformIP4Address *addr4; addr4 = nm_ip4_config_get_address (ip4_config, 0); - g_assert (addr4); /* checked for > 1 address above */ - priv->lookup_addr = g_inet_address_new_from_bytes ((guint8 *) &addr4->address, - G_SOCKET_FAMILY_IPV4); - } else if (ip6_config) { + G_SOCKET_FAMILY_IPV4); + } else if (ip6_config && nm_ip6_config_get_num_addresses (ip6_config) > 0) { const NMPlatformIP6Address *addr6; addr6 = nm_ip6_config_get_address (ip6_config, 0); - g_assert (addr6); /* checked for > 1 address above */ - priv->lookup_addr = g_inet_address_new_from_bytes ((guint8 *) &addr6->address, - G_SOCKET_FAMILY_IPV6); + G_SOCKET_FAMILY_IPV6); } else { - /* Should never get here... */ - g_warn_if_reached (); + /* No valid IP config; fall back to localhost.localdomain */ _set_hostname (policy, NULL, "no IP config"); return; }