Skip to contentSkip to conversation

arpd: use designated initializers for msghdr structure

PATCH RESEND Superseded Dario Binacchi 2 participants 3 messages Sep 22, 2024 – Sep 27, 2024

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH RESEND 1/2 arpd: use designated initializers for msghdr structure

This patch fixes the following error:

arpd.c:442:17: error: initialization of 'int' from 'void *' makes integer from pointer without a cast [-Wint-conversion]
442 | NULL, 0,

raised by Buildroot autobuilder [1].

In the case in question, the analysis of socket.h [2] containing the msghdr structure shows that it has been modified with the addition of padding fields, which cause the compilation error. The use of designated initializers allows the issue to be fixed.

struct msghdr {
	void *msg_name;
	socklen_t msg_namelen;
	struct iovec *msg_iov;
#if __LONG_MAX > 0x7fffffff && __BYTE_ORDER == __BIG_ENDIAN
	int __pad1;
#endif
	int msg_iovlen;
#if __LONG_MAX > 0x7fffffff && __BYTE_ORDER == __LITTLE_ENDIAN
	int __pad1;
#endif
	void *msg_control;
#if __LONG_MAX > 0x7fffffff && __BYTE_ORDER == __BIG_ENDIAN
	int __pad2;
#endif
	socklen_t msg_controllen;
#if __LONG_MAX > 0x7fffffff && __BYTE_ORDER == __LITTLE_ENDIAN
	int __pad2;
#endif
	int msg_flags;
};

[1] http://autobuild.buildroot.org/results/e4cdfa38ae9578992f1c0ff5c4edae3cc0836e3c/
[2] iproute2/host/mips64-buildroot-linux-musl/sysroot/usr/include/sys/socket.h

---
 misc/arpd.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
misc/arpd.c +4 -4
index e77ef53928a2..b4935c23eebb 100644
@@ -437,10 +437,10 @@ static void get_kern_msg(void)
 	struct iovec iov;
 	char   buf[8192];
 	struct msghdr msg = {
-		(void *)&nladdr, sizeof(nladdr),
-		&iov,	1,
-		NULL,	0,
-		0
+		.msg_name = &nladdr, .msg_namelen = sizeof(nladdr),
+		.msg_iov = &iov, .msg_iovlen = 1,
+		.msg_control = (void *)NULL, .msg_controllen = 0,
+		.msg_flags = 0
 	};
 
 	iov.iov_base = buf;
Signature · 1 line
2.43.0
#1 lore raw

PATCH RESEND 2/2 arpd: drop useless initializers

It is useless to initialize the fields of a structure to their default values. This patch keeps the initialization only for those fields that are not set to their default values.

---
 misc/arpd.c | 2 --
 1 file changed, 2 deletions(-)
misc/arpd.c +0 -2
index b4935c23eebb..a2ae76a41f8d 100644
@@ -439,8 +439,6 @@ static void get_kern_msg(void)
 	struct msghdr msg = {
 		.msg_name = &nladdr, .msg_namelen = sizeof(nladdr),
 		.msg_iov = &iov, .msg_iovlen = 1,
-		.msg_control = (void *)NULL, .msg_controllen = 0,
-		.msg_flags = 0
 	};
 
 	iov.iov_base = buf;
Signature · 1 line
2.43.0
#2 lore raw
In reply to Dario Binacchi
Quoted 25 lines · On Sun, 22 Sep 2024 16:46:12 +0200 Dario Binacchi…

This patch fixes the following error:

arpd.c:442:17: error: initialization of 'int' from 'void *' makes integer from pointer without a cast [-Wint-conversion]
442 | NULL, 0,

raised by Buildroot autobuilder [1].

In the case in question, the analysis of socket.h [2] containing the msghdr structure shows that it has been modified with the addition of padding fields, which cause the compilation error. The use of designated initializers allows the issue to be fixed.

struct msghdr {
	void *msg_name;
	socklen_t msg_namelen;
	struct iovec *msg_iov;
#if __LONG_MAX > 0x7fffffff && __BYTE_ORDER == __BIG_ENDIAN
	int __pad1;
#endif
	int msg_iovlen;
#if __LONG_MAX > 0x7fffffff && __BYTE_ORDER == __LITTLE_ENDIAN
	int __pad1;
#endif
	void *msg_control;
#if __LONG_MAX > 0x7fffffff && __BYTE_ORDER == __BIG_ENDIAN
Quoted 8 lines
	int __pad2;
#endif
	socklen_t msg_controllen;
#if __LONG_MAX > 0x7fffffff && __BYTE_ORDER == __LITTLE_ENDIAN
	int __pad2;
#endif
	int msg_flags;
};

That is a really bad idea to put extra padding in there.

Quoted 14 lines

[1] http://autobuild.buildroot.org/results/e4cdfa38ae9578992f1c0ff5c4edae3cc0836e3c/
[2] iproute2/host/mips64-buildroot-linux-musl/sysroot/usr/include/sys/socket.h

---
 misc/arpd.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
misc/arpd.c +0 -0
index e77ef53928a2..b4935c23eebb 100644

misc/arpd.c static void get_kern_msg(void) · line 440

Quoted 8 lines
misc/arpd.c +1 -4
@@ -437,10 +437,10 @@ static void get_kern_msg(void)
 	struct iovec iov;
 	char   buf[8192];
 	struct msghdr msg = {
-		(void *)&nladdr, sizeof(nladdr),
-		&iov,	1,
-		NULL,	0,
-		0
+		.msg_name = &nladdr, .msg_namelen = sizeof(nladdr),

When converting, to named initializer, please put one per line like other code does in iproute.

misc/arpd.c static void get_kern_msg(void) · line 442

Quoted 2 lines
misc/arpd.c +2 -0
+		.msg_iov = &iov, .msg_iovlen = 1,
+		.msg_control = (void *)NULL, .msg_controllen = 0,

The C standard says that NULL can be used for void *, cast there is unnecessary.

Quoted 4 lines
misc/arpd.c +1 -0
+		.msg_flags = 0
 	};
 
 	iov.iov_base = buf;