Skip to contentSkip to conversation

netlink: Make groups check less stupid in netlink_bind()

PATCH Applied Dmitry Safonov 2 participants 2 messages Sep 3, 2018 – Sep 6, 2018

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

PATCH 1/1 netlink: Make groups check less stupid in netlink_bind()

As Linus noted, the test for 0 is needless, groups type can follow the usual kernel style and 8*sizeof(unsigned long) is BITS_PER_LONG:

Quoted 12 lines

The code [..] isn't technically incorrect...
But it is stupid.
Why stupid? Because the test for 0 is pointless.

Just doing

       if (nlk->ngroups < 8*sizeof(groups))
               groups &= (1UL << nlk->ngroups) - 1;

would have been fine and more understandable, since the "mask by shift count" already does the right thing for a ngroups value of 0. Now that test for zero makes me go "what's special about zero?". It turns out that the answer to that is "nothing".

[..]

Quoted 4 lines

The type of "groups" is kind of silly too.

Yeah, "long unsigned int" isn't _technically_ wrong. But we normally call that type "unsigned long".

Cleanup my piece of pointlessness.

Cc "David S. Miller" <[email protected]>
Cc Herbert Xu <[email protected]>
Cc Steffen Klassert <[email protected]>
---
 net/netlink/af_netlink.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
net/netlink/af_netlink.c +2 -4
index 930d17fa906c..b4a29bcc33b9 100644
@@ -993,7 +993,7 @@ static int netlink_bind(struct socket *sock, struct sockaddr *addr,
 	struct netlink_sock *nlk = nlk_sk(sk);
 	struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
 	int err = 0;
-	long unsigned int groups = nladdr->nl_groups;
+	unsigned long groups = nladdr->nl_groups;
 	bool bound;
 
 	if (addr_len < sizeof(struct sockaddr_nl))
@@ -1011,9 +1011,7 @@ static int netlink_bind(struct socket *sock, struct sockaddr *addr,
 			return err;
 	}
 
-	if (nlk->ngroups == 0)
-		groups = 0;
-	else if (nlk->ngroups < 8*sizeof(groups))
+	if (nlk->ngroups < BITS_PER_LONG)
 		groups &= (1UL << nlk->ngroups) - 1;
 
 	bound = nlk->bound;
Signature · 1 line
2.13.6
maintainer Applied
#1 lore raw

From: Dmitry Safonov <[email protected]>
Date: Mon, 3 Sep 2018 22:40:51 +0100

Quoted 21 lines

As Linus noted, the test for 0 is needless, groups type can follow the usual kernel style and 8*sizeof(unsigned long) is BITS_PER_LONG:

Quoted 12 lines

The code [..] isn't technically incorrect...
But it is stupid.
Why stupid? Because the test for 0 is pointless.

Just doing

       if (nlk->ngroups < 8*sizeof(groups))
               groups &= (1UL << nlk->ngroups) - 1;

would have been fine and more understandable, since the "mask by shift count" already does the right thing for a ngroups value of 0. Now that test for zero makes me go "what's special about zero?". It turns out that the answer to that is "nothing".

[..]

Quoted 4 lines

The type of "groups" is kind of silly too.

Yeah, "long unsigned int" isn't _technically_ wrong. But we normally call that type "unsigned long".

Quoted 8 lines

Cleanup my piece of pointlessness.

Cc "David S. Miller" <[email protected]>
Cc Herbert Xu <[email protected]>
Cc Steffen Klassert <[email protected]>

Applied to net-next, thanks.