Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 12 Feb 2022 00:10:36 GMT
From:      John Baldwin <jhb@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: dba02df30d53 - main - Cast pointer to uintptr_t to avoid alignment warnings.
Message-ID:  <202202120010.21C0AatV017560@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by jhb:

URL: https://cgit.FreeBSD.org/src/commit/?id=dba02df30d536922727a7ea509514462452a247a

commit dba02df30d536922727a7ea509514462452a247a
Author:     John Baldwin <jhb@FreeBSD.org>
AuthorDate: 2022-02-12 00:04:52 +0000
Commit:     John Baldwin <jhb@FreeBSD.org>
CommitDate: 2022-02-12 00:04:52 +0000

    Cast pointer to uintptr_t to avoid alignment warnings.
    
    Both struct ip and struct udphdr both have an aligment of 2, but the
    cast from struct ip to a uint32_t pointer confused GCC 9 into raising
    the required alignment to 4 and then raising a
    -Waddress-of-packed-member error when casting to struct udphdr.
    
    Reviewed by:    imp
    Differential Revision:  https://reviews.freebsd.org/D31941
---
 tests/sys/netinet/libalias/util.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/tests/sys/netinet/libalias/util.c b/tests/sys/netinet/libalias/util.c
index 681c3b20ee41..14ba196a59a5 100644
--- a/tests/sys/netinet/libalias/util.c
+++ b/tests/sys/netinet/libalias/util.c
@@ -109,9 +109,9 @@ ip_packet(u_char protocol, size_t len)
 
 struct udphdr *
 set_udp(struct ip *p, u_short sport, u_short dport) {
-	uint32_t *up = (void *)p;
-	struct udphdr *u = (void *)&(up[p->ip_hl]);
-	int payload = ntohs(p->ip_len) - 4*p->ip_hl;
+	int hlen = p->ip_hl << 2;
+	struct udphdr *u = (struct udphdr *)((uintptr_t)p + hlen);
+	int payload = ntohs(p->ip_len) - hlen;
 
 	REQUIRE(payload >= (int)sizeof(*u));
 	p->ip_p = IPPROTO_UDP;



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202202120010.21C0AatV017560>