sysdeps-unix: Don't leak struct addrinfo on OOM during connect()

If we ran out of memory while handling connect() errors, we didn't
free the linked list of struct addrinfo. Move their cleanup to the
"out" phase of the function so that we always do it.

While I'm there, change the iterator variable tmp to be const, to make
it more obvious that we aren't meant to free it.

This is similar to commit 00badeba (!143) in the corresponding Windows
code path, but with some refactoring.

Signed-off-by: Simon McVittie <smcv@collabora.com>
This commit is contained in:
Simon McVittie 2020-03-11 13:12:22 +00:00
parent 041cadb738
commit 682e619196

View file

@ -1411,7 +1411,8 @@ _dbus_connect_tcp_socket_with_nonce (const char *host,
DBusSocket fd = DBUS_SOCKET_INIT;
int res;
struct addrinfo hints;
struct addrinfo *ai, *tmp;
struct addrinfo *ai = NULL;
const struct addrinfo *tmp;
DBusError *connect_error;
_DBUS_ASSERT_ERROR_IS_CLEAR(error);
@ -1450,7 +1451,6 @@ _dbus_connect_tcp_socket_with_nonce (const char *host,
{
if (!_dbus_open_socket (&fd.fd, tmp->ai_family, SOCK_STREAM, 0, error))
{
freeaddrinfo(ai);
_DBUS_ASSERT_ERROR_IS_SET(error);
_dbus_socket_invalidate (&fd);
goto out;
@ -1491,7 +1491,6 @@ _dbus_connect_tcp_socket_with_nonce (const char *host,
break;
}
freeaddrinfo(ai);
if (!_dbus_socket_is_valid (fd))
{
@ -1523,6 +1522,9 @@ _dbus_connect_tcp_socket_with_nonce (const char *host,
}
out:
if (ai != NULL)
freeaddrinfo (ai);
while ((connect_error = _dbus_list_pop_first (&connect_errors)))
{
dbus_error_free (connect_error);