mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] [PATCH] Work around -Wcast-align and -Wsign-compare warnings in sys/socket.h
@ 2022-02-16 20:27 Colin Cross
  0 siblings, 0 replies; only message in thread
From: Colin Cross @ 2022-02-16 20:27 UTC (permalink / raw)
  To: musl; +Cc: Christian Eggers, Colin Cross

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


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-02-16 20:27 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-16 20:27 [musl] [PATCH] Work around -Wcast-align and -Wsign-compare warnings in sys/socket.h Colin Cross

Code repositories for project(s) associated with this inbox:

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).