Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [day] [month] [year] [list]
Date: Wed, 16 Feb 2022 12:27:05 -0800
From: Colin Cross <ccross@...gle.com>
To: musl@...ts.openwall.com
Cc: Christian Eggers <ceggers@...i.de>, Colin Cross <ccross@...gle.com>
Subject: [PATCH] Work around -Wcast-align and -Wsign-compare warnings in sys/socket.h

Clang produces -Wcast-align and -Wsign-compare warnings when using
CMSG_NXTHDR:

./nettest.cpp:5:12: warning: cast from 'unsigned char *' to 'struct cmsghdr *' increases required alignment from 1 to 4 [-Wcast-align]
    return CMSG_NXTHDR(mhdr, cmsg);
           ^~~~~~~~~~~~~~~~~~~~~~~
/usr/include/sys/socket.h:270:8: note: expanded from macro 'CMSG_NXTHDR'
        ? 0 : (struct cmsghdr *)__CMSG_NEXT(cmsg))
              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
./nettest.cpp:5:12: warning: comparison of integers of different signs: 'unsigned long' and 'long' [-Wsign-compare]
    return CMSG_NXTHDR(mhdr, cmsg);
           ^~~~~~~~~~~~~~~~~~~~~~~
/usr/include/sys/socket.h:269:44: note: expanded from macro 'CMSG_NXTHDR'
        __CMSG_LEN(cmsg) + sizeof(struct cmsghdr) >= __MHDR_END(mhdr) - (unsigned char *)(cmsg) \
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2 warnings generated.

Clang should really be hiding the warnings when the header is included from a
directory specified with -isystem, but it doesn't.  This has resulted in
patches to many projects work around the issue:
https://github.com/openembedded/meta-openembedded/commit/cfa3f070885482dc2f0c3e13fe70f20fad46e35e
https://github.com/openembedded/meta-openembedded/commit/cfb432a714457d2fd66a02fe7f1340094742ba69
https://github.com/dotnet/corefx/pull/6263
https://github.com/dotnet/corefx/pull/12520

It also occurs in the Android codebase in multiple projects.

It has come up on the mailing list multiple times:
https://www.openwall.com/lists/musl/2016/10/10/1
https://www.openwall.com/lists/musl/2018/03/06/4
https://www.openwall.com/lists/musl/2022/02/16/7

Pragmatically work around the clang bug by fixing the warnings, casting
through void * to avoid the -Wcast-align warning and casting to
size_t to avoid the -Wsign-compare warning.
---
 include/sys/socket.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/include/sys/socket.h b/include/sys/socket.h
index 38f5bb17..b604f6cd 100644
--- a/include/sys/socket.h
+++ b/include/sys/socket.h
@@ -353,8 +353,8 @@ 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) \
-	? 0 : (struct cmsghdr *)__CMSG_NEXT(cmsg))
+	__CMSG_LEN(cmsg) + sizeof(struct cmsghdr) >= (size_t)(__MHDR_END(mhdr) - (unsigned char *)(cmsg)) \
+	? 0 : (struct cmsghdr *)(void *)__CMSG_NEXT(cmsg))
 #define CMSG_FIRSTHDR(mhdr) ((size_t) (mhdr)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (mhdr)->msg_control : (struct cmsghdr *) 0)
 
 #define CMSG_ALIGN(len) (((len) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1))
-- 
2.35.1.265.g69c8d7142f-goog

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.