]> git.ipfire.org Git - thirdparty/dbus.git/commitdiff
_dbus_transport_new_for_socket: Simplify with _DBUS_STRING_INIT_INVALID
authorSimon McVittie <smcv@collabora.com>
Tue, 21 Nov 2017 12:40:07 +0000 (12:40 +0000)
committerSimon McVittie <smcv@collabora.com>
Fri, 24 Nov 2017 12:17:21 +0000 (12:17 +0000)
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

dbus/dbus-transport-socket.c

index 05acde0c34771b1c46164a3e14542b38981e3baf..fc9418238d27ba12d1ae8f189d68e2a68f50d55e 100644 (file)
@@ -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;
 }