gproxyaddressenumerator: Strengthen some type assertions

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 <pwithnall@gnome.org>

Helps: #1767
This commit is contained in:
Philip Withnall 2024-04-09 23:22:42 +01:00
parent 05158475e9
commit 4894168631
No known key found for this signature in database
GPG Key ID: DCDF5885B1F3ED73

View File

@ -285,14 +285,10 @@ g_proxy_address_enumerator_next (GSocketAddressEnumerator *enumerator,
{ {
dest_hostname = g_strdup (priv->dest_hostname); 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); 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); inetsaddr = G_INET_SOCKET_ADDRESS (priv->proxy_address);
inetaddr = g_inet_socket_address_get_address (inetsaddr); inetaddr = g_inet_socket_address_get_address (inetsaddr);
@ -363,7 +359,6 @@ return_result (GTask *task)
{ {
GProxyAddressEnumeratorPrivate *priv = g_task_get_task_data (task); GProxyAddressEnumeratorPrivate *priv = g_task_get_task_data (task);
GSocketAddress *result; GSocketAddress *result;
gboolean is_inet_socket_address;
if (strcmp ("direct", priv->proxy_type) == 0) if (strcmp ("direct", priv->proxy_type) == 0)
{ {
@ -395,13 +390,7 @@ return_result (GTask *task)
} }
dest_protocol = g_uri_parse_scheme (priv->dest_uri); dest_protocol = g_uri_parse_scheme (priv->dest_uri);
is_inet_socket_address = G_IS_INET_SOCKET_ADDRESS (priv->proxy_address); g_assert (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);
inetsaddr = G_INET_SOCKET_ADDRESS (priv->proxy_address); inetsaddr = G_INET_SOCKET_ADDRESS (priv->proxy_address);
inetaddr = g_inet_socket_address_get_address (inetsaddr); inetaddr = g_inet_socket_address_get_address (inetsaddr);