]> git.ipfire.org Git - thirdparty/dbus.git/commitdiff
sysdeps-unix: Don't leak struct addrinfo on OOM during connect()
authorSimon McVittie <smcv@collabora.com>
Wed, 11 Mar 2020 13:12:22 +0000 (13:12 +0000)
committerSimon McVittie <smcv@collabora.com>
Wed, 11 Mar 2020 13:12:55 +0000 (13:12 +0000)
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>
dbus/dbus-sysdeps-unix.c

index c91c05ef4933025a3255b1dc88480f7a94022824..4607f53708e2cb11eee99dc0223b9399a719bad4 100644 (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);