|
|
Message-ID: <20260610135519.3723009-2-vad.sol@proton.me>
Date: Wed, 10 Jun 2026 13:55:45 +0000
From: vad <vad.sol@...ton.me>
To: musl@...ts.openwall.com
Cc: vad <vad.sol@...ton.me>
Subject: [PATCH] sys/socket.h: fix the clang sign-compare diagnostic in CMSG_NXTHDR
Clang emits a sign-compare diagnostic whenever signed integer is
compared with an unsigned one. That happens with the comparison done
in CMSG_NXTHDR:
../common/connection.c:154:55: error: comparison of integers of different signs: 'unsigned long' and 'long' [-Werror,-Wsign-compare]
154 | for (cmsg = CMSG_FIRSTHDR(msg); cmsg != NULL; cmsg = CMSG_NXTHDR(msg, cmsg)) {
| ^~~~~~~~~~~~~~~~~~~~~~
/usr/include/sys/socket.h:358:44: note: expanded from macro 'CMSG_NXTHDR'
358 | __CMSG_LEN(cmsg) + sizeof(struct cmsghdr) >= __MHDR_END(mhdr) - (unsigned char *)(cmsg) \
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The reason is that the right value is a result of a subtraction, and
therefore its type is a signed ptrdiff_t (long):
__MHDR_END(mhdr) - (unsigned char*)(cmsg)
Even though CMSG_NXTHDR is a part of a system header, it's a macro, so
clang ends up expanding them in
This was already discussed back in 2024[0] and a common workaround, used
libpcap[1] and serenity[2], is to surpress the diagnostic right around
the macro call.
To remove the necessity of such changes in every project that uses the
macro and supports clang builds, and given that the subtraction will
never return a negative result, explicitly cast it to size_t.
[0] https://www.openwall.com/lists/musl/2024/03/29/3
[1] https://github.com/the-tcpdump-group/libpcap/commit/36d63625cfbf544aa0b6fd4b1e95a299d9822bb8
[2] https://github.com/SerenityOS/serenity/pull/26110/files#diff-cac3e0872aab71c09d3f66bbcab3e582af5533d303064c074d25e9509ee3e3cf
---
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.52.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.