peer/new: create the socket last
The socket consumes the file descriptor, so make sure to only create
it once the function can no longer fail.
@@ -377,10 +377,6 @@ int peer_new(Peer **peerp,
reply_registry_init(&peer->replies_outgoing);
peer->replies_incoming = (CList)C_LIST_INIT(peer->replies_incoming);
- r = socket_new(&peer->socket, fd);
- if (r < 0)
- return r;
-
r = dispatch_file_init(&peer->dispatch_file,
&bus->dispatcher,
&bus->ready_list,
@@ -390,6 +386,10 @@ int peer_new(Peer **peerp,
if (r < 0)
return r;
+ r = socket_new(&peer->socket, fd);
+ if (r < 0)
+ return r;
+
peer->id = bus->peers.ids++;
*peerp = peer;
