Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [day] [month] [year] [list]
Message-ID: <20260318184510.41025-2-jeremymosas@gmail.com>
Date: Wed, 18 Mar 2026 19:45:11 +0100
From: statzitz <jeremy.primard01@...il.com>
To: musl@...ts.openwall.com
Cc: statzitz <jeremy.primard01@...il.com>
Subject: [PATCH v2] sys/socket.h: Fixed -Wsign-compare in musl CMSG_NXTHDR() macro

From: statzitz <jeremy.primard01@...il.com>

__CMSG_LEN(cmsg) is a size_t
sizeof(struct cmsghdr) is a size_t

So __CMSG_LEN(cmsg) + sizeof(struct cmsghdr) is also a size_t

__MHDR_END(mhdr) is an unsigned char pointer
(unsigned char *)(cmsg) is also an unsigned char pointer

so __MHDR_END(mhdr) - (unsigned char *)(cmsg) will not result in a
size_t, but in a ptrdiff_t (signed). The compiler think that there
is a world where the result can be negative, so we needs to
explicitely state that the result is a size_t (unsigned).
---
 include/sys/socket.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/sys/socket.h b/include/sys/socket.h
index 6dc1e40a..0b39d835 100644
--- a/include/sys/socket.h
+++ b/include/sys/socket.h
@@ -355,7 +355,7 @@ struct linger {
 
 #define CMSG_DATA(cmsg) ((unsigned char *) (((struct cmsghdr *)(cmsg)) + 1))
 #define CMSG_NXTHDR(mhdr, cmsg) ((cmsg)->cmsg_len < sizeof (struct cmsghdr) || \
-	__CMSG_LEN(cmsg) + sizeof(struct cmsghdr) >= __MHDR_END(mhdr) - (unsigned char *)(cmsg) \
+	__CMSG_LEN(cmsg) + sizeof(struct cmsghdr) >= (size_t)(__MHDR_END(mhdr) - (unsigned char *)(cmsg)) \
 	? 0 : (struct cmsghdr *)__CMSG_NEXT(cmsg))
 #define CMSG_FIRSTHDR(mhdr) ((size_t) (mhdr)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (mhdr)->msg_control : (struct cmsghdr *) 0)
 
-- 
2.53.0

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.