From: Dmitry V. Levin Date: Sun, 16 Sep 2018 21:32:37 +0000 (+0000) Subject: Workaround signedness bugs in system NLMSG_OK reported by -Wsign-compare X-Git-Tag: v4.25~12 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=96df4b36f6346f4474d2bb8ef6efd785aa7586e0;p=strace Workaround signedness bugs in system NLMSG_OK reported by -Wsign-compare Introduce a replacement for NLMSG_OK provided by since that system macro contains signedness bugs that are not going to be fixed. * netlink.h: Include . (is_nlmsg_ok): New static inline function. * socketutils.c (receive_responses): Use it instead of NLMSG_OK. * tests/netlink_inet_diag.c (check_responses): Likewise. * tests/netlink_netlink_diag.c (check_responses): Likewise. * tests/netlink_unix_diag.c (check_responses): Likewise. Closes: https://github.com/strace/strace/issues/79 --- diff --git a/netlink.h b/netlink.h index 42e7802e..48abe0b8 100644 --- a/netlink.h +++ b/netlink.h @@ -29,6 +29,7 @@ #ifndef STRACE_NETLINK_H #define STRACE_NETLINK_H +#include #include #include @@ -63,4 +64,12 @@ # define NLA_TYPE_MASK ~(NLA_F_NESTED | NLA_F_NET_BYTEORDER) #endif +static inline bool +is_nlmsg_ok(const struct nlmsghdr *const nlh, const ssize_t len) +{ + return len >= (ssize_t) sizeof(*nlh) + && nlh->nlmsg_len >= sizeof(*nlh) + && (size_t) len >= nlh->nlmsg_len; +} + #endif /* !STRACE_NETLINK_H */ diff --git a/socketutils.c b/socketutils.c index aa0752c3..c3f28a1b 100644 --- a/socketutils.c +++ b/socketutils.c @@ -237,9 +237,9 @@ receive_responses(struct tcb *tcp, const int fd, const unsigned long inode, } const struct nlmsghdr *h = &hdr_buf.hdr; - if (!NLMSG_OK(h, ret)) + if (!is_nlmsg_ok(h, ret)) return false; - for (; NLMSG_OK(h, ret); h = NLMSG_NEXT(h, ret)) { + for (; is_nlmsg_ok(h, ret); h = NLMSG_NEXT(h, ret)) { if (h->nlmsg_type != expected_msg_type) return false; const int rc = parser(NLMSG_DATA(h), diff --git a/tests/netlink_inet_diag.c b/tests/netlink_inet_diag.c index 8b3d8af0..dc990aeb 100644 --- a/tests/netlink_inet_diag.c +++ b/tests/netlink_inet_diag.c @@ -100,8 +100,8 @@ check_responses(const int fd) perror_msg_and_skip("recvmsg"); struct nlmsghdr *h = &hdr_buf.hdr; - if (!NLMSG_OK(h, ret)) - error_msg_and_skip("!NLMSG_OK"); + if (!is_nlmsg_ok(h, ret)) + error_msg_and_skip("!is_nlmsg_ok"); if (h->nlmsg_type == NLMSG_ERROR) { const struct nlmsgerr *err = NLMSG_DATA(h); if (h->nlmsg_len < NLMSG_LENGTH(sizeof(*err))) diff --git a/tests/netlink_netlink_diag.c b/tests/netlink_netlink_diag.c index a682f7c6..ee651eef 100644 --- a/tests/netlink_netlink_diag.c +++ b/tests/netlink_netlink_diag.c @@ -100,8 +100,8 @@ check_responses(const int fd) perror_msg_and_skip("recvmsg"); struct nlmsghdr *h = &hdr_buf.hdr; - if (!NLMSG_OK(h, ret)) - error_msg_and_skip("!NLMSG_OK"); + if (!is_nlmsg_ok(h, ret)) + error_msg_and_skip("!is_nlmsg_ok"); if (h->nlmsg_type == NLMSG_ERROR) { const struct nlmsgerr *err = NLMSG_DATA(h); if (h->nlmsg_len < NLMSG_LENGTH(sizeof(*err))) diff --git a/tests/netlink_unix_diag.c b/tests/netlink_unix_diag.c index e1509238..f74e7306 100644 --- a/tests/netlink_unix_diag.c +++ b/tests/netlink_unix_diag.c @@ -104,8 +104,8 @@ check_responses(const int fd) perror_msg_and_skip("recvmsg"); struct nlmsghdr *h = &hdr_buf.hdr; - if (!NLMSG_OK(h, ret)) - error_msg_and_skip("!NLMSG_OK"); + if (!is_nlmsg_ok(h, ret)) + error_msg_and_skip("!is_nlmsg_ok"); if (h->nlmsg_type == NLMSG_ERROR) { const struct nlmsgerr *err = NLMSG_DATA(h); if (h->nlmsg_len < NLMSG_LENGTH(sizeof(*err)))