From: David Gibson <david@gibson.dropbear.id.au>
To: passt-dev@passt.top
Subject: [PATCH 16/28] Avoid ugly 'end' members in netlink structures
Date: Wed, 28 Sep 2022 14:33:27 +1000 [thread overview]
Message-ID: <20220928043339.613538-17-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20220928043339.613538-1-david@gibson.dropbear.id.au>
[-- Attachment #1: Type: text/plain, Size: 3310 bytes --]
We use a number of complex structures to format messages to send to
netlink. In some cases we add imaginary 'end' members not because they
actually mean something on the wire, but so that we can use offsetof() on
the member to determine the relevant size.
Adding extra things to the structures for this is kinda nasty. We can use
a different construct with offsetof and sizeof to avoid them. As a bonus
this removes some cppcheck warnings about unused struct members.
Signed-off-by: David Gibson <david(a)gibson.dropbear.id.au>
---
netlink.c | 19 ++++++++++---------
1 file changed, 10 insertions(+), 9 deletions(-)
diff --git a/netlink.c b/netlink.c
index 6f7ada8..15ce213 100644
--- a/netlink.c
+++ b/netlink.c
@@ -206,7 +206,6 @@ void nl_route(int ns, unsigned int ifi, sa_family_t af, void *gw)
uint32_t d;
struct rtattr rta_gw;
uint32_t a;
- uint8_t end;
} r4;
} set;
} req = {
@@ -234,7 +233,8 @@ void nl_route(int ns, unsigned int ifi, sa_family_t af, void *gw)
if (af == AF_INET6) {
size_t rta_len = RTA_LENGTH(sizeof(req.set.r6.d));
- req.nlh.nlmsg_len = sizeof(req);
+ req.nlh.nlmsg_len = offsetof(struct req_t, set.r6)
+ + sizeof(req.set.r6);
req.set.r6.rta_dst.rta_type = RTA_DST;
req.set.r6.rta_dst.rta_len = rta_len;
@@ -245,7 +245,8 @@ void nl_route(int ns, unsigned int ifi, sa_family_t af, void *gw)
} else {
size_t rta_len = RTA_LENGTH(sizeof(req.set.r4.d));
- req.nlh.nlmsg_len = offsetof(struct req_t, set.r4.end);
+ req.nlh.nlmsg_len = offsetof(struct req_t, set.r4)
+ + sizeof(req.set.r4);
req.set.r4.rta_dst.rta_type = RTA_DST;
req.set.r4.rta_dst.rta_len = rta_len;
@@ -312,8 +313,6 @@ void nl_addr(int ns, unsigned int ifi, sa_family_t af,
uint32_t l;
struct rtattr rta_a;
uint32_t a;
-
- uint8_t end;
} a4;
struct {
struct rtattr rta_l;
@@ -343,7 +342,8 @@ void nl_addr(int ns, unsigned int ifi, sa_family_t af,
if (af == AF_INET6) {
size_t rta_len = RTA_LENGTH(sizeof(req.set.a6.l));
- req.nlh.nlmsg_len = sizeof(req);
+ req.nlh.nlmsg_len = offsetof(struct req_t, set.a6)
+ + sizeof(req.set.a6);
memcpy(&req.set.a6.l, addr, sizeof(req.set.a6.l));
req.set.a6.rta_l.rta_len = rta_len;
@@ -354,7 +354,8 @@ void nl_addr(int ns, unsigned int ifi, sa_family_t af,
} else {
size_t rta_len = RTA_LENGTH(sizeof(req.set.a4.l));
- req.nlh.nlmsg_len = offsetof(struct req_t, set.a4.end);
+ req.nlh.nlmsg_len = offsetof(struct req_t, set.a4)
+ + sizeof(req.set.a4);
req.set.a4.l = req.set.a4.a = *(uint32_t *)addr;
req.set.a4.rta_l.rta_len = rta_len;
@@ -425,7 +426,6 @@ void nl_link(int ns, unsigned int ifi, void *mac, int up, int mtu)
unsigned char mac[ETH_ALEN];
struct {
unsigned int mtu;
- uint8_t end;
} mtu;
} set;
} req = {
@@ -457,7 +457,8 @@ void nl_link(int ns, unsigned int ifi, void *mac, int up, int mtu)
}
if (mtu) {
- req.nlh.nlmsg_len = offsetof(struct req_t, set.mtu.end);
+ req.nlh.nlmsg_len = offsetof(struct req_t, set.mtu)
+ + sizeof(req.set.mtu);
req.set.mtu.mtu = mtu;
req.rta.rta_type = IFLA_MTU;
req.rta.rta_len = RTA_LENGTH(sizeof(unsigned int));
--
@@ -206,7 +206,6 @@ void nl_route(int ns, unsigned int ifi, sa_family_t af, void *gw)
uint32_t d;
struct rtattr rta_gw;
uint32_t a;
- uint8_t end;
} r4;
} set;
} req = {
@@ -234,7 +233,8 @@ void nl_route(int ns, unsigned int ifi, sa_family_t af, void *gw)
if (af == AF_INET6) {
size_t rta_len = RTA_LENGTH(sizeof(req.set.r6.d));
- req.nlh.nlmsg_len = sizeof(req);
+ req.nlh.nlmsg_len = offsetof(struct req_t, set.r6)
+ + sizeof(req.set.r6);
req.set.r6.rta_dst.rta_type = RTA_DST;
req.set.r6.rta_dst.rta_len = rta_len;
@@ -245,7 +245,8 @@ void nl_route(int ns, unsigned int ifi, sa_family_t af, void *gw)
} else {
size_t rta_len = RTA_LENGTH(sizeof(req.set.r4.d));
- req.nlh.nlmsg_len = offsetof(struct req_t, set.r4.end);
+ req.nlh.nlmsg_len = offsetof(struct req_t, set.r4)
+ + sizeof(req.set.r4);
req.set.r4.rta_dst.rta_type = RTA_DST;
req.set.r4.rta_dst.rta_len = rta_len;
@@ -312,8 +313,6 @@ void nl_addr(int ns, unsigned int ifi, sa_family_t af,
uint32_t l;
struct rtattr rta_a;
uint32_t a;
-
- uint8_t end;
} a4;
struct {
struct rtattr rta_l;
@@ -343,7 +342,8 @@ void nl_addr(int ns, unsigned int ifi, sa_family_t af,
if (af == AF_INET6) {
size_t rta_len = RTA_LENGTH(sizeof(req.set.a6.l));
- req.nlh.nlmsg_len = sizeof(req);
+ req.nlh.nlmsg_len = offsetof(struct req_t, set.a6)
+ + sizeof(req.set.a6);
memcpy(&req.set.a6.l, addr, sizeof(req.set.a6.l));
req.set.a6.rta_l.rta_len = rta_len;
@@ -354,7 +354,8 @@ void nl_addr(int ns, unsigned int ifi, sa_family_t af,
} else {
size_t rta_len = RTA_LENGTH(sizeof(req.set.a4.l));
- req.nlh.nlmsg_len = offsetof(struct req_t, set.a4.end);
+ req.nlh.nlmsg_len = offsetof(struct req_t, set.a4)
+ + sizeof(req.set.a4);
req.set.a4.l = req.set.a4.a = *(uint32_t *)addr;
req.set.a4.rta_l.rta_len = rta_len;
@@ -425,7 +426,6 @@ void nl_link(int ns, unsigned int ifi, void *mac, int up, int mtu)
unsigned char mac[ETH_ALEN];
struct {
unsigned int mtu;
- uint8_t end;
} mtu;
} set;
} req = {
@@ -457,7 +457,8 @@ void nl_link(int ns, unsigned int ifi, void *mac, int up, int mtu)
}
if (mtu) {
- req.nlh.nlmsg_len = offsetof(struct req_t, set.mtu.end);
+ req.nlh.nlmsg_len = offsetof(struct req_t, set.mtu)
+ + sizeof(req.set.mtu);
req.set.mtu.mtu = mtu;
req.rta.rta_type = IFLA_MTU;
req.rta.rta_len = RTA_LENGTH(sizeof(unsigned int));
--
2.37.3
next prev parent reply other threads:[~2022-09-28 4:33 UTC|newest]
Thread overview: 41+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-28 4:33 [PATCH 00/28] Fixes for static checkers David Gibson
2022-09-28 4:33 ` [PATCH 01/28] Clean up parsing of port ranges David Gibson
2022-09-28 20:57 ` Stefano Brivio
2022-09-29 1:04 ` David Gibson
2022-09-28 4:33 ` [PATCH 02/28] clang-tidy: Suppress warning about unchecked error in logfn macro David Gibson
2022-09-28 4:33 ` [PATCH 03/28] clang-tidy: Fix spurious null pointer warning in pasta_start_ns() David Gibson
2022-09-28 4:33 ` [PATCH 04/28] clang-tidy: Remove duplicate #include from icmp.c David Gibson
2022-09-28 4:33 ` [PATCH 05/28] Catch failures when installing signal handlers David Gibson
2022-09-28 4:33 ` [PATCH 06/28] Pack DHCPv6 "on wire" structures David Gibson
2022-09-28 4:33 ` [PATCH 07/28] Clean up parsing in conf_runas() David Gibson
2022-09-28 20:57 ` Stefano Brivio
2022-09-29 1:44 ` David Gibson
2022-09-28 4:33 ` [PATCH 08/28] cppcheck: Reduce scope of some variables David Gibson
2022-09-28 4:33 ` [PATCH 09/28] Don't shadow 'i' in conf_ports() David Gibson
2022-09-28 4:33 ` [PATCH 10/28] Don't shadow global function names David Gibson
2022-09-28 4:33 ` [PATCH 11/28] Stricter checking for nsholder.c David Gibson
2022-09-28 4:33 ` [PATCH 12/28] cppcheck: Work around false positive NULL pointer dereference error David Gibson
2022-09-28 4:33 ` [PATCH 13/28] cppcheck: Use inline suppression for ffsl() David Gibson
2022-09-28 4:33 ` [PATCH 14/28] cppcheck: Use inline suppressions for qrap.c David Gibson
2022-09-28 4:33 ` [PATCH 15/28] cppcheck: Use inline suppression for strtok() in conf.c David Gibson
2022-09-28 4:33 ` David Gibson [this message]
2022-09-28 4:33 ` [PATCH 17/28] cppcheck: Broaden suppression for unused struct members David Gibson
2022-09-28 4:33 ` [PATCH 18/28] cppcheck: Remove localtime suppression for pcap.c David Gibson
2022-09-28 4:33 ` [PATCH 19/28] qrap: Handle case of PATH environment variable being unset David Gibson
2022-09-28 4:33 ` [PATCH 20/28] cppcheck: Suppress same-value-in-ternary branches warning David Gibson
2022-09-28 20:58 ` Stefano Brivio
2022-09-29 1:00 ` David Gibson
2022-09-28 4:33 ` [PATCH 21/28] cppcheck: Suppress NULL pointer warning in tcp_sock_consume() David Gibson
2022-09-28 20:58 ` Stefano Brivio
2022-09-29 1:07 ` David Gibson
2022-09-28 4:33 ` [PATCH 22/28] Regenerate seccomp.h if seccomp.sh changes David Gibson
2022-09-28 4:33 ` [PATCH 23/28] cppcheck: Avoid errors due to zeroes in bitwise ORs David Gibson
2022-09-28 4:33 ` [PATCH 24/28] cppcheck: Remove unused knownConditionTrueFalse suppression David Gibson
2022-09-28 20:58 ` Stefano Brivio
2022-09-29 1:24 ` David Gibson
2022-09-28 4:33 ` [PATCH 25/28] cppcheck: Remove unused objectIndex suppressions David Gibson
2022-09-28 20:58 ` Stefano Brivio
2022-09-29 1:12 ` David Gibson
2022-09-28 4:33 ` [PATCH 26/28] cppcheck: Remove unused va_list_usedBeforeStarted suppression David Gibson
2022-09-28 4:33 ` [PATCH 27/28] Mark unused functions for cppcheck David Gibson
2022-09-28 4:33 ` [PATCH 28/28] cppcheck: Remove unused unmatchedSuppression suppressions David Gibson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220928043339.613538-17-david@gibson.dropbear.id.au \
--to=david@gibson.dropbear.id.au \
--cc=passt-dev@passt.top \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://passt.top/passt
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).