Skip to contentSkip to conversation

net/socket.c::sys_bind() cleanup.

PATCH In review [email protected] 5 participants 5 messages Nov 22, 2004 – Nov 25, 2004

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 10/13 net/socket.c::sys_bind() cleanup.

From: "Luiz Fernando N. Capitulino" <[email protected]>

net/socket.c::sys_bind() is a bit complex function, the patch below makes it more clear.

---

 25-akpm/net/socket.c |   29 +++++++++++++++++------------
 1 files changed, 17 insertions(+), 12 deletions(-)
net/socket.c renamed from net/socket.c~net-socketcsys_bind-cleanup +17 -12
diff -puN net/socket.c~net-socketcsys_bind-cleanup net/socket.c
@@ -1286,18 +1286,23 @@ asmlinkage long sys_bind(int fd, struct 
 	char address[MAX_SOCK_ADDR];
 	int err;
 
-	if((sock = sockfd_lookup(fd,&err))!=NULL)
-	{
-		if((err=move_addr_to_kernel(umyaddr,addrlen,address))>=0) {
-			err = security_socket_bind(sock, (struct sockaddr *)address, addrlen);
-			if (err) {
-				sockfd_put(sock);
-				return err;
-			}
-			err = sock->ops->bind(sock, (struct sockaddr *)address, addrlen);
-		}
-		sockfd_put(sock);
-	}			
+	sock = sockfd_lookup(fd, &err);
+	if (!sock)
+		goto out;
+
+	err = move_addr_to_kernel(umyaddr, addrlen, address);
+	if (err)
+		goto out_put;
+
+	err = security_socket_bind(sock, (struct sockaddr *)address, addrlen);
+	if (err)
+		goto out_put;
+
+	err = sock->ops->bind(sock, (struct sockaddr *)address, addrlen);
+
+ out_put:
+	sockfd_put(sock);
+ out:
 	return err;
 }
 

_

maintainer
#1 lore raw
Quoted 7 lines · On Sun, 21 Nov 2004 22:43:56 -0800 [email protected] wrote:

From: "Luiz Fernando N. Capitulino" <[email protected]>

net/socket.c::sys_bind() is a bit complex function, the patch below makes it more clear.

This was commented on to be buggy, or at least change behavior. The "if (err >= 0)" tests were changed to flat "if (err)" tests.

#2 lore raw
Quoted 9 lines · David S. Miller
Quoted 5 lines · On Sun, 21 Nov 2004 22:43:56 -0800 [email protected] wrote:

From: "Luiz Fernando N. Capitulino" <[email protected]>

net/socket.c::sys_bind() is a bit complex function, the patch below makes it more clear.

Quoted 6 lines
Quoted 2 lines

This was commented on to be buggy, or at least change behavior. The "if (err >= 0)" tests were changed to flat "if (err)" tests.

It doesn't a buggy or change behaivor.

What happens here is that move_addr_to_kernel() returns 0
success and -EINVAL or -EFAULT on error. Thus, change from "if (err >= 0)" to "if (err)" is safe.

Also, it was discussed and ACK'ed by James Morris:

http://lkml.org/lkml/2004/11/16/339

The real problem here I've made a bad patch
description. I'll try to make it better next time.

thanks,

Signature · 1 line
Luiz Fernando N. Capitulino
#3 lore raw

It's just a style issue, so maybe you'll disagree, but I prefer:

        if (!sock)
                return err;

to your

        if (!sock)
                goto out;

...
out:
return err;

I think "return err" is more readable than "goto out/return err"
for
that path and having another "return err" for the out-with-put case isn't a
terrible thing.

                                +-DLS
#4 lore raw
Quoted 13 lines · David Stevens

It's just a style issue, so maybe you'll disagree, but I prefer:

       if (!sock)
               return err;

to your

       if (!sock)
               goto out;

...
out:
return err;

Quoted 5 lines

I think "return err" is more readable than "goto out/return err"
for
that path and having another "return err" for the out-with-put case isn't a
terrible thing.

When used in isolation you would be right. However when there are multiple exit points then the goto is better because it means that we have only one exit path. When you have multiple exit paths it's very easy to get memory leaks and missing unlock's.

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt