From 2dd871ff86933cdee615bb1879ef6fe4da6aedb1 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Tue, 21 Nov 2017 12:40:07 +0000 Subject: [PATCH 03/12] _dbus_transport_new_for_socket: Simplify with _DBUS_STRING_INIT_INVALID This is one of the few places that has test coverage for all the OOM code paths. It was also one of the worst (most complicated) error-unwinding locations, with labels failed_0 up to failed_4. Signed-off-by: Simon McVittie --- dbus/dbus-transport-socket.c | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/dbus/dbus-transport-socket.c b/dbus/dbus-transport-socket.c index 05acde0c..fc941823 100644 --- a/dbus/dbus-transport-socket.c +++ b/dbus/dbus-transport-socket.c @@ -1293,35 +1293,40 @@ _dbus_transport_new_for_socket (DBusSocket fd, const DBusString *address) { DBusTransportSocket *socket_transport; - + DBusString invalid = _DBUS_STRING_INIT_INVALID; + socket_transport = dbus_new0 (DBusTransportSocket, 1); if (socket_transport == NULL) return NULL; + /* So they can be "freed" without error */ + socket_transport->encoded_outgoing = invalid; + socket_transport->encoded_incoming = invalid; + if (!_dbus_string_init (&socket_transport->encoded_outgoing)) - goto failed_0; + goto failed; if (!_dbus_string_init (&socket_transport->encoded_incoming)) - goto failed_1; + goto failed; socket_transport->write_watch = _dbus_watch_new (_dbus_socket_get_pollable (fd), DBUS_WATCH_WRITABLE, FALSE, NULL, NULL, NULL); if (socket_transport->write_watch == NULL) - goto failed_2; + goto failed; socket_transport->read_watch = _dbus_watch_new (_dbus_socket_get_pollable (fd), DBUS_WATCH_READABLE, FALSE, NULL, NULL, NULL); if (socket_transport->read_watch == NULL) - goto failed_3; + goto failed; if (!_dbus_transport_init_base (&socket_transport->base, &socket_vtable, server_guid, address)) - goto failed_4; + goto failed; #ifdef HAVE_UNIX_FD_PASSING _dbus_auth_set_unix_fd_possible(socket_transport->base.auth, _dbus_socket_can_pass_unix_fd(fd)); @@ -1336,17 +1341,21 @@ _dbus_transport_new_for_socket (DBusSocket fd, return (DBusTransport*) socket_transport; - failed_4: - _dbus_watch_invalidate (socket_transport->read_watch); - _dbus_watch_unref (socket_transport->read_watch); - failed_3: - _dbus_watch_invalidate (socket_transport->write_watch); - _dbus_watch_unref (socket_transport->write_watch); - failed_2: +failed: + if (socket_transport->read_watch != NULL) + { + _dbus_watch_invalidate (socket_transport->read_watch); + _dbus_watch_unref (socket_transport->read_watch); + } + + if (socket_transport->write_watch != NULL) + { + _dbus_watch_invalidate (socket_transport->write_watch); + _dbus_watch_unref (socket_transport->write_watch); + } + _dbus_string_free (&socket_transport->encoded_incoming); - failed_1: _dbus_string_free (&socket_transport->encoded_outgoing); - failed_0: dbus_free (socket_transport); return NULL; } -- 2.15.0