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 a0d9310..5d8d3ed 100644
--- a/socketutils.c
+++ b/socketutils.c
@@ -187,13 +187,17 @@
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 @@
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 342be07..2332e20 100644
--- a/tests/netlink_inet_diag.c
+++ b/tests/netlink_inet_diag.c
@@ -75,13 +75,17 @@
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 @@
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 0afdb5d..fc32822 100644
--- a/tests/netlink_netlink_diag.c
+++ b/tests/netlink_netlink_diag.c
@@ -80,13 +80,17 @@
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 @@
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 e5ef6fa..b91cbbb 100644
--- a/tests/netlink_unix_diag.c
+++ b/tests/netlink_unix_diag.c
@@ -83,13 +83,17 @@
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 @@
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) {