Skip to contentSkip to conversation

netlink: gc useless variable in nlmsg_attrdata()

PATCH In review Alexey Dobriyan 2 participants 4 messages Aug 12, 2021 – Aug 13, 2021

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

PATCH 1/1 netlink: gc useless variable in nlmsg_attrdata()

Kernel permits pointer arithmetic on "void*" so might as well use it without casts back and forth.

---

 include/net/netlink.h |    3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
include/net/netlink.h +1 -2
@@ -587,8 +587,7 @@ static inline int nlmsg_len(const struct nlmsghdr *nlh)
 static inline struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh,
 					    int hdrlen)
 {
-	unsigned char *data = nlmsg_data(nlh);
-	return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen));
+	return nlmsg_data(nlh) + NLMSG_ALIGN(hdrlen);
 }
 
 /**
maintainer
#1 lore raw
Quoted 2 lines · On Fri, 13 Aug 2021 00:24:01 +0300 Alexey Dobriyan wrote:

Kernel permits pointer arithmetic on "void*" so might as well use it without casts back and forth.

But why change existing code? It's perfectly fine, right?

Quoted 12 lines
include/net/netlink.h +1 -2
@@ -587,8 +587,7 @@ static inline int nlmsg_len(const struct nlmsghdr *nlh)
 static inline struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh,
 					    int hdrlen)
 {
-	unsigned char *data = nlmsg_data(nlh);
-	return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen));
+	return nlmsg_data(nlh) + NLMSG_ALIGN(hdrlen);
 }
 
 /**
#2 lore raw
Quoted 5 lines · Jakub Kicinski
Quoted 2 lines · On Fri, 13 Aug 2021 00:24:01 +0300 Alexey Dobriyan wrote:

Kernel permits pointer arithmetic on "void*" so might as well use it without casts back and forth.

But why change existing code? It's perfectly fine, right?

It is harder to read (marginally of course).

Quoted 13 lines
Quoted 12 lines
include/net/netlink.h +1 -2
@@ -587,8 +587,7 @@ static inline int nlmsg_len(const struct nlmsghdr *nlh)
 static inline struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh,
 					    int hdrlen)
 {
-	unsigned char *data = nlmsg_data(nlh);
-	return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen));
+	return nlmsg_data(nlh) + NLMSG_ALIGN(hdrlen);
 }
 
 /**
maintainer
#3 lore raw
Quoted 8 lines · On Fri, 13 Aug 2021 08:27:30 +0300 Alexey Dobriyan wrote:
Quoted 5 lines · Jakub Kicinski
Quoted 2 lines · On Fri, 13 Aug 2021 00:24:01 +0300 Alexey Dobriyan wrote:

Kernel permits pointer arithmetic on "void*" so might as well use it without casts back and forth.

But why change existing code? It's perfectly fine, right?

It is harder to read (marginally of course).

TBH I prefer the current code, I don't have to wonder what type nlmsg_data() returns and whether it's okay to do void* arithmetic in this header (if it's uAPI). Sorry :(