From 581344c17daa7bb77b7456644020bc709cacc9a4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Marc-Andr=C3=A9=20Lureau?= Date: Tue, 8 Feb 2022 14:57:05 +0400 Subject: [PATCH] dbus: set the socket as invalid in _dbus_close_socket() MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This can simplify error handling in many situation where a socket is returned, such as in the following commits. Signed-off-by: Marc-André Lureau --- dbus/dbus-sysdeps-unix.c | 15 ++++++++++++--- dbus/dbus-sysdeps-win.c | 4 +++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/dbus/dbus-sysdeps-unix.c b/dbus/dbus-sysdeps-unix.c index 152a3862f..c77adc097 100644 --- a/dbus/dbus-sysdeps-unix.c +++ b/dbus/dbus-sysdeps-unix.c @@ -297,8 +297,12 @@ _dbus_open_unix_socket (int *fd, } /** - * Closes a socket. Should not be used on non-socket - * file descriptors or handles. + * Closes a socket and invalidates it. Should not be used on non-socket file + * descriptors or handles. + * + * If an error is detected, this function returns #FALSE and sets the error, but + * the socket is still closed and invalidated. Callers can use the error in a + * diagnostic message, but should not retry closing the socket. * * @param fd the socket * @param error return location for an error @@ -308,10 +312,15 @@ dbus_bool_t _dbus_close_socket (DBusSocket *fd, DBusError *error) { + dbus_bool_t rv; + _dbus_assert (fd != NULL); _DBUS_ASSERT_ERROR_IS_CLEAR (error); - return _dbus_close (fd->fd, error); + rv = _dbus_close (fd->fd, error); + _dbus_socket_invalidate (fd); + + return rv; } /** diff --git a/dbus/dbus-sysdeps-win.c b/dbus/dbus-sysdeps-win.c index 445662148..d15b6991e 100644 --- a/dbus/dbus-sysdeps-win.c +++ b/dbus/dbus-sysdeps-win.c @@ -483,7 +483,7 @@ _dbus_write_socket (DBusSocket fd, /** - * Closes a file descriptor. + * Closes a socket and invalidates it. * * @param fd the file descriptor * @param error error object @@ -507,10 +507,12 @@ _dbus_close_socket (DBusSocket *fd, dbus_set_error (error, _dbus_error_from_errno (errno), "Could not close socket: socket=%Iu, , %s", fd->sock, _dbus_strerror_from_errno ()); + _dbus_socket_invalidate (fd); return FALSE; } _dbus_verbose ("socket=%Iu, \n", fd->sock); + _dbus_socket_invalidate (fd); return TRUE; } -- 2.47.3