Discussion:
[musl] Mistake in RTA_OK macro
Ondřej Jirman
2018-11-20 23:29:50 UTC
Permalink
Hello,

This line:

grep -R RTA_OK

src/network/netlink.h:#define RTA_OK(nlh,end) ((char*)(end)-(char*)(rta) >= sizeof(struct rtattr))

only works by accident, because all uses of NLMSG_RTAOK reference rta variable:

src/network/getifaddrs.c: for (rta = NLMSG_RTA(h, sizeof(*ifi)); NLMSG_RTAOK(rta, h); rta = RTA_NEXT(rta)) {
src/network/getifaddrs.c: for (rta = NLMSG_RTA(h, sizeof(*ifi)); NLMSG_RTAOK(rta, h); rta = RTA_NEXT(rta)) {
src/network/getifaddrs.c: for (rta = NLMSG_RTA(h, sizeof(*ifa)); NLMSG_RTAOK(rta, h); rta = RTA_NEXT(rta)) {
src/network/netlink.h:#define NLMSG_RTAOK(rta,nlh) RTA_OK(rta,NLMSG_DATAEND(nlh))
src/network/if_nameindex.c: for (; NLMSG_RTAOK(rta, h); rta = RTA_NEXT(rta)) {

I suggest:

-#define RTA_OK(nlh,end) ((char*)(end)-(char*)(rta) >= sizeof(struct rtattr))
+#define RTA_OK(rta,end) ((char*)(end)-(char*)(rta) >= sizeof(struct rtattr))

thank you and regards,
o.
m***@megous.com
2018-11-21 16:29:21 UTC
Permalink
From: Ondrej Jirman <***@megous.com>

The wrong name works only by accident.
---
src/network/netlink.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

This is a proper patch for the issue I've described in a recent e-mail
to the mailing list.

diff --git a/src/network/netlink.h b/src/network/netlink.h
index 38acb178..873fabe2 100644
--- a/src/network/netlink.h
+++ b/src/network/netlink.h
@@ -86,7 +86,7 @@ struct ifaddrmsg {
#define RTA_DATALEN(rta) ((rta)->rta_len-sizeof(struct rtattr))
#define RTA_DATAEND(rta) ((char*)(rta)+(rta)->rta_len)
#define RTA_NEXT(rta) (struct rtattr*)((char*)(rta)+NETLINK_ALIGN((rta)->rta_len))
-#define RTA_OK(nlh,end) ((char*)(end)-(char*)(rta) >= sizeof(struct rtattr))
+#define RTA_OK(rta,end) ((char*)(end)-(char*)(rta) >= sizeof(struct rtattr))

#define NLMSG_RTA(nlh,len) ((void*)((char*)(nlh)+sizeof(struct nlmsghdr)+NETLINK_ALIGN(len)))
#define NLMSG_RTAOK(rta,nlh) RTA_OK(rta,NLMSG_DATAEND(nlh))
--
2.19.1
Loading...