summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon McVittie <smcv@collabora.com>2017-11-21 12:40:07 +0000
committerSimon McVittie <smcv@collabora.com>2017-11-24 12:17:21 +0000
commitfa123560d33a2f90b2ee9927aa4cb610f4fa85f1 (patch)
tree102cbc7fbccc6c757f61ea6ac2b4bf84a526400d
parente49a21e357ef9de4c87cd6ab014d207a55bdb4e5 (diff)
_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 <smcv@collabora.com> Reviewed-by: Philip Withnall <withnall@endlessm.com> Bug: https://bugs.freedesktop.org/show_bug.cgi?id=89104
-rw-r--r--dbus/dbus-transport-socket.c39
1 files changed, 24 insertions, 15 deletions
diff --git a/dbus/dbus-transport-socket.c b/dbus/dbus-transport-socket.c
index 05acde0c3..fc9418238 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;
}