Skip to content

Commit

Permalink
af_unix: Clean up error paths in unix_stream_connect().
Browse files Browse the repository at this point in the history
The label order is weird in unix_stream_connect(), and all NULL checks
are unnecessary if reordered.

Let's clean up the error paths to make it easy to set a drop reason
for each path.

While at it, a comment with the old style is updated.

Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
  • Loading branch information
Kuniyuki Iwashima authored and Paolo Abeni committed Dec 17, 2024
1 parent 34c899a commit e26ee0a
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions net/unix/af_unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -1563,32 +1563,30 @@ static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr,
timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);

/* First of all allocate resources.
If we will make it after state is locked,
we will have to recheck all again in any case.
* If we will make it after state is locked,
* we will have to recheck all again in any case.
*/

/* create new sock for complete connection */
newsk = unix_create1(net, NULL, 0, sock->type);
if (IS_ERR(newsk)) {
err = PTR_ERR(newsk);
newsk = NULL;
goto out;
}

/* Allocate skb for sending to listening sock */
skb = sock_wmalloc(newsk, 1, 0, GFP_KERNEL);
if (!skb) {
err = -ENOMEM;
goto out;
goto out_free_sk;
}

restart:
/* Find listening sock. */
other = unix_find_other(net, sunaddr, addr_len, sk->sk_type);
if (IS_ERR(other)) {
err = PTR_ERR(other);
other = NULL;
goto out;
goto out_free_skb;
}

unix_state_lock(other);
Expand All @@ -1613,11 +1611,12 @@ static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr,
}

timeo = unix_wait_for_peer(other, timeo);
sock_put(other);

err = sock_intr_errno(timeo);
if (signal_pending(current))
goto out;
sock_put(other);
goto out_free_skb;

goto restart;
}

Expand Down Expand Up @@ -1702,15 +1701,13 @@ static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr,
return 0;

out_unlock:
if (other)
unix_state_unlock(other);

out:
unix_state_unlock(other);
sock_put(other);
out_free_skb:
kfree_skb(skb);
if (newsk)
unix_release_sock(newsk, 0);
if (other)
sock_put(other);
out_free_sk:
unix_release_sock(newsk, 0);
out:
return err;
}

Expand Down

0 comments on commit e26ee0a

Please sign in to comment.