From 489416863143b25e0da5bdba56ee78f451a612db Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Tue, 9 Apr 2024 23:22:42 +0100 Subject: [PATCH] gproxyaddressenumerator: Strengthen some type assertions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit scan-build was complaining that `dest_hostname` and `dest_protocol` were used after being freed, which could potentially happen if the code is built with `G_DISABLE_CHECKS`. This is a false positive, because the state of types in the program should be the same regardless of whether `G_DISABLE_CHECKS` is used. However, the code did smell. If we are trying to free things and return gracefully if the underlying socket address enumerator returns something of the wrong type, why not free the rest of the function’s state, or skip the invalid address and move on to the next one? Or if we are trying to make an assertion, why bother freeing some temporary data at all? This halfway house doesn’t make sense. So turn the `g_return_val_if_fail()` into a full assertion. Signed-off-by: Philip Withnall Helps: #1767 --- gio/gproxyaddressenumerator.c | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/gio/gproxyaddressenumerator.c b/gio/gproxyaddressenumerator.c index 0d1539a9d..d173b61c3 100644 --- a/gio/gproxyaddressenumerator.c +++ b/gio/gproxyaddressenumerator.c @@ -285,14 +285,10 @@ g_proxy_address_enumerator_next (GSocketAddressEnumerator *enumerator, { dest_hostname = g_strdup (priv->dest_hostname); } + + g_assert (G_IS_INET_SOCKET_ADDRESS (priv->proxy_address)); + dest_protocol = g_uri_parse_scheme (priv->dest_uri); - - if (!G_IS_INET_SOCKET_ADDRESS (priv->proxy_address)) - { - g_free (dest_hostname); - g_free (dest_protocol); - } - g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (priv->proxy_address), NULL); inetsaddr = G_INET_SOCKET_ADDRESS (priv->proxy_address); inetaddr = g_inet_socket_address_get_address (inetsaddr); @@ -363,7 +359,6 @@ return_result (GTask *task) { GProxyAddressEnumeratorPrivate *priv = g_task_get_task_data (task); GSocketAddress *result; - gboolean is_inet_socket_address; if (strcmp ("direct", priv->proxy_type) == 0) { @@ -395,13 +390,7 @@ return_result (GTask *task) } dest_protocol = g_uri_parse_scheme (priv->dest_uri); - is_inet_socket_address = G_IS_INET_SOCKET_ADDRESS (priv->proxy_address); - if (!is_inet_socket_address) - { - g_free (dest_hostname); - g_free (dest_protocol); - } - g_return_if_fail (is_inet_socket_address); + g_assert (G_IS_INET_SOCKET_ADDRESS (priv->proxy_address)); inetsaddr = G_INET_SOCKET_ADDRESS (priv->proxy_address); inetaddr = g_inet_socket_address_get_address (inetsaddr);