test: Fix VLA declarations
C++ doesn't support VLAs, but none of the uses were actually intended to
be dynamic. Instead, refactor out array lengths into constexpr
expressions.
Change-Id: I60dfd5c8552a249a7b28b663e7d4e5d1fdfa96e0
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/test/test_neighbor.cpp b/test/test_neighbor.cpp
index 6b14912..0f0abc4 100644
--- a/test/test_neighbor.cpp
+++ b/test/test_neighbor.cpp
@@ -71,9 +71,10 @@
msg.ndm_state = NUD_REACHABLE;
ether_addr mac = {{0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa}};
rtattr lladdr{};
- lladdr.rta_len = RTA_LENGTH(sizeof(mac));
+ constexpr size_t len = RTA_LENGTH(sizeof(mac));
+ lladdr.rta_len = len;
lladdr.rta_type = NDA_LLADDR;
- char lladdrbuf[RTA_ALIGN(lladdr.rta_len)];
+ char lladdrbuf[RTA_ALIGN(len)];
std::memset(lladdrbuf, '\0', sizeof(lladdrbuf));
std::memcpy(lladdrbuf, &lladdr, sizeof(lladdr));
std::memcpy(RTA_DATA(lladdrbuf), &mac, sizeof(mac));
@@ -99,9 +100,10 @@
in_addr addr;
ASSERT_EQ(1, inet_pton(msg.ndm_family, "192.168.10.1", &addr));
rtattr dst{};
- dst.rta_len = RTA_LENGTH(sizeof(addr));
+ constexpr size_t len = RTA_LENGTH(sizeof(addr));
+ dst.rta_len = len;
dst.rta_type = NDA_DST;
- char dstbuf[RTA_ALIGN(dst.rta_len)];
+ char dstbuf[RTA_ALIGN(len)];
std::memset(dstbuf, '\0', sizeof(dstbuf));
std::memcpy(dstbuf, &dst, sizeof(dst));
std::memcpy(RTA_DATA(dstbuf), &addr, sizeof(addr));
@@ -131,9 +133,10 @@
in_addr addr;
ASSERT_EQ(1, inet_pton(msg.ndm_family, "192.168.10.1", &addr));
rtattr dst{};
- dst.rta_len = RTA_LENGTH(sizeof(addr));
+ constexpr size_t len = RTA_LENGTH(sizeof(addr));
+ dst.rta_len = len;
dst.rta_type = NDA_DST;
- char dstbuf[RTA_ALIGN(dst.rta_len)];
+ char dstbuf[RTA_ALIGN(len)];
std::memset(dstbuf, '\0', sizeof(dstbuf));
std::memcpy(dstbuf, &dst, sizeof(dst));
std::memcpy(RTA_DATA(dstbuf), &addr, sizeof(addr));
@@ -167,9 +170,10 @@
in_addr addr;
ASSERT_EQ(1, inet_pton(msg.ndm_family, "192.168.10.1", &addr));
rtattr dst{};
- dst.rta_len = RTA_LENGTH(sizeof(addr));
+ constexpr size_t len = RTA_LENGTH(sizeof(addr));
+ dst.rta_len = len;
dst.rta_type = NDA_DST;
- char dstbuf[RTA_ALIGN(dst.rta_len)];
+ char dstbuf[RTA_ALIGN(len)];
std::memset(dstbuf, '\0', sizeof(dstbuf));
std::memcpy(dstbuf, &dst, sizeof(dst));
std::memcpy(RTA_DATA(dstbuf), &addr, sizeof(addr));
@@ -202,18 +206,20 @@
msg.ndm_ifindex = 1;
ether_addr mac = {{0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa}};
rtattr lladdr{};
- lladdr.rta_len = RTA_LENGTH(sizeof(mac));
+ constexpr size_t lladdr_len = RTA_LENGTH(sizeof(mac));
+ lladdr.rta_len = lladdr_len;
lladdr.rta_type = NDA_LLADDR;
- char lladdrbuf[RTA_ALIGN(lladdr.rta_len)];
+ char lladdrbuf[RTA_ALIGN(lladdr_len)];
std::memset(lladdrbuf, '\0', sizeof(lladdrbuf));
std::memcpy(lladdrbuf, &lladdr, sizeof(lladdr));
std::memcpy(RTA_DATA(lladdrbuf), &mac, sizeof(mac));
in6_addr addr;
ASSERT_EQ(1, inet_pton(msg.ndm_family, "fd00::1", &addr));
rtattr dst{};
- dst.rta_len = RTA_LENGTH(sizeof(addr));
+ constexpr size_t dst_len = RTA_LENGTH(sizeof(addr));
+ dst.rta_len = dst_len;
dst.rta_type = NDA_DST;
- char dstbuf[RTA_ALIGN(dst.rta_len)];
+ char dstbuf[RTA_ALIGN(dst_len)];
std::memset(dstbuf, '\0', sizeof(dstbuf));
std::memcpy(dstbuf, &dst, sizeof(dst));
std::memcpy(RTA_DATA(dstbuf), &addr, sizeof(addr));
diff --git a/test/test_netlink.cpp b/test/test_netlink.cpp
index 6e5a198..b3538f2 100644
--- a/test/test_netlink.cpp
+++ b/test/test_netlink.cpp
@@ -86,9 +86,10 @@
{
nlmsgerr ack{};
nlmsghdr hdr{};
- hdr.nlmsg_len = NLMSG_LENGTH(sizeof(ack));
+ constexpr size_t len = NLMSG_LENGTH(sizeof(ack));
+ hdr.nlmsg_len = len;
hdr.nlmsg_type = NLMSG_ERROR;
- char buf[NLMSG_ALIGN(hdr.nlmsg_len)];
+ char buf[NLMSG_ALIGN(len)];
std::memcpy(buf, &hdr, sizeof(hdr));
std::memcpy(NLMSG_DATA(buf), &ack, sizeof(ack));
std::string_view data(reinterpret_cast<char*>(&buf), sizeof(buf));
@@ -107,9 +108,10 @@
nlmsgerr err{};
err.error = EINVAL;
nlmsghdr hdr{};
- hdr.nlmsg_len = NLMSG_LENGTH(sizeof(err));
+ constexpr size_t len = NLMSG_LENGTH(sizeof(err));
+ hdr.nlmsg_len = len;
hdr.nlmsg_type = NLMSG_ERROR;
- char buf[NLMSG_ALIGN(hdr.nlmsg_len)];
+ char buf[NLMSG_ALIGN(len)];
std::memcpy(buf, &hdr, sizeof(hdr));
std::memcpy(NLMSG_DATA(buf), &err, sizeof(err));
std::string_view data(reinterpret_cast<char*>(&buf), sizeof(buf));