From 2b2d9f76e3c3ae35d9faadccce3d8298c656c540 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Thu, 8 Mar 2018 14:36:17 +0000 Subject: [PATCH 6/7] _dbus_connect_tcp_socket_with_nonce: Always goto out This will make it easier to do single-exit-path cleanup. Signed-off-by: Simon McVittie --- dbus/dbus-sysdeps-unix.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/dbus/dbus-sysdeps-unix.c b/dbus/dbus-sysdeps-unix.c index f2ea9023..e1275715 100644 --- a/dbus/dbus-sysdeps-unix.c +++ b/dbus/dbus-sysdeps-unix.c @@ -1563,7 +1563,8 @@ _dbus_connect_tcp_socket_with_nonce (const char *host, _dbus_error_from_errno (errno), "Failed to lookup host/port: \"%s:%s\": %s (%d)", host, port, gai_strerror(res), res); - return _dbus_socket_get_invalid (); + _dbus_socket_invalidate (&fd); + goto out; } tmp = ai; @@ -1573,7 +1574,8 @@ _dbus_connect_tcp_socket_with_nonce (const char *host, { freeaddrinfo(ai); _DBUS_ASSERT_ERROR_IS_SET(error); - return _dbus_socket_get_invalid (); + _dbus_socket_invalidate (&fd); + goto out; } _DBUS_ASSERT_ERROR_IS_CLEAR(error); @@ -1581,7 +1583,7 @@ _dbus_connect_tcp_socket_with_nonce (const char *host, { saved_errno = errno; _dbus_close (fd.fd, NULL); - fd.fd = -1; + _dbus_socket_invalidate (&fd); tmp = tmp->ai_next; continue; } @@ -1590,13 +1592,13 @@ _dbus_connect_tcp_socket_with_nonce (const char *host, } freeaddrinfo(ai); - if (fd.fd == -1) + if (!_dbus_socket_is_valid (fd)) { dbus_set_error (error, _dbus_error_from_errno (saved_errno), "Failed to connect to socket \"%s:%s\" %s", host, port, _dbus_strerror(saved_errno)); - return _dbus_socket_get_invalid (); + goto out; } if (noncefile != NULL) @@ -1610,16 +1612,19 @@ _dbus_connect_tcp_socket_with_nonce (const char *host, if (!ret) { _dbus_close (fd.fd, NULL); - return _dbus_socket_get_invalid (); + _dbus_socket_invalidate (&fd); + goto out; } } if (!_dbus_set_fd_nonblocking (fd.fd, error)) { _dbus_close (fd.fd, NULL); - return _dbus_socket_get_invalid (); + _dbus_socket_invalidate (&fd); + goto out; } +out: return fd; } -- 2.16.2