From: Dmitry V. Levin Date: Thu, 19 May 2016 01:23:40 +0000 (+0000) Subject: Fix one more code pattern that might break gcc strict aliasing rules X-Git-Tag: v4.12~112 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7b6979701b5ddda47ba53f82381a5f412708b0ad;p=strace Fix one more code pattern that might break gcc strict aliasing rules * socketutils.c (receive_responses): Turn static buffer into a union to avoid breaking of gcc strict aliasing rules. * tests/netlink_inet_diag.c (check_responses): Likewise. * tests/netlink_netlink_diag.c (check_responses): Likewise. * tests/netlink_unix_diag.c (check_responses): Likewise. --- diff --git a/socketutils.c b/socketutils.c index a0d9310d..5d8d3ed9 100644 --- a/socketutils.c +++ b/socketutils.c @@ -187,13 +187,17 @@ receive_responses(const int fd, const unsigned long inode, int (* parser) (const char *, const void *, int, unsigned long)) { - static long buf[8192 / sizeof(long)]; + static union { + struct nlmsghdr hdr; + long buf[8192 / sizeof(long)]; + } hdr_buf; + struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK }; struct iovec iov = { - .iov_base = buf, - .iov_len = sizeof(buf) + .iov_base = hdr_buf.buf, + .iov_len = sizeof(hdr_buf.buf) }; int flags = 0; @@ -212,7 +216,7 @@ receive_responses(const int fd, const unsigned long inode, return false; } - const struct nlmsghdr *h = (struct nlmsghdr *) buf; + const struct nlmsghdr *h = &hdr_buf.hdr; if (!NLMSG_OK(h, ret)) return false; for (; NLMSG_OK(h, ret); h = NLMSG_NEXT(h, ret)) { diff --git a/tests/netlink_inet_diag.c b/tests/netlink_inet_diag.c index 342be072..2332e203 100644 --- a/tests/netlink_inet_diag.c +++ b/tests/netlink_inet_diag.c @@ -75,13 +75,17 @@ send_query(const int fd, const int family, const int proto) static void check_responses(const int fd) { - static long buf[8192 / sizeof(long)]; + static union { + struct nlmsghdr hdr; + long buf[8192 / sizeof(long)]; + } hdr_buf; + struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK }; struct iovec iov = { - .iov_base = buf, - .iov_len = sizeof(buf) + .iov_base = hdr_buf.buf, + .iov_len = sizeof(hdr_buf.buf) }; struct msghdr msg = { .msg_name = (void *) &nladdr, @@ -94,7 +98,7 @@ check_responses(const int fd) if (ret <= 0) perror_msg_and_skip("recvmsg"); - struct nlmsghdr *h = (struct nlmsghdr *) buf; + struct nlmsghdr *h = &hdr_buf.hdr; if (!NLMSG_OK(h, ret)) error_msg_and_skip("!NLMSG_OK"); if (h->nlmsg_type == NLMSG_ERROR) { diff --git a/tests/netlink_netlink_diag.c b/tests/netlink_netlink_diag.c index 0afdb5d7..fc32822b 100644 --- a/tests/netlink_netlink_diag.c +++ b/tests/netlink_netlink_diag.c @@ -80,13 +80,17 @@ send_query(const int fd) static void check_responses(const int fd) { - static long buf[8192 / sizeof(long)]; + static union { + struct nlmsghdr hdr; + long buf[8192 / sizeof(long)]; + } hdr_buf; + struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK }; struct iovec iov = { - .iov_base = buf, - .iov_len = sizeof(buf) + .iov_base = hdr_buf.buf, + .iov_len = sizeof(hdr_buf.buf) }; struct msghdr msg = { .msg_name = (void *) &nladdr, @@ -99,7 +103,7 @@ check_responses(const int fd) if (ret <= 0) perror_msg_and_skip("recvmsg"); - struct nlmsghdr *h = (struct nlmsghdr *) buf; + struct nlmsghdr *h = &hdr_buf.hdr; if (!NLMSG_OK(h, ret)) error_msg_and_skip("!NLMSG_OK"); if (h->nlmsg_type == NLMSG_ERROR) { diff --git a/tests/netlink_unix_diag.c b/tests/netlink_unix_diag.c index e5ef6fab..b91cbbbe 100644 --- a/tests/netlink_unix_diag.c +++ b/tests/netlink_unix_diag.c @@ -83,13 +83,17 @@ send_query(const int fd) static void check_responses(const int fd) { - static long buf[8192 / sizeof(long)]; + static union { + struct nlmsghdr hdr; + long buf[8192 / sizeof(long)]; + } hdr_buf; + struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK }; struct iovec iov = { - .iov_base = buf, - .iov_len = sizeof(buf) + .iov_base = hdr_buf.buf, + .iov_len = sizeof(hdr_buf.buf) }; struct msghdr msg = { .msg_name = (void *) &nladdr, @@ -102,7 +106,7 @@ check_responses(const int fd) if (ret <= 0) perror_msg_and_skip("recvmsg"); - struct nlmsghdr *h = (struct nlmsghdr *) buf; + struct nlmsghdr *h = &hdr_buf.hdr; if (!NLMSG_OK(h, ret)) error_msg_and_skip("!NLMSG_OK"); if (h->nlmsg_type == NLMSG_ERROR) {