mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Colin Cross <>
Cc: Christian Eggers <>, Colin Cross <>
Subject: [musl] [PATCH] Work around -Wcast-align and -Wsign-compare warnings in sys/socket.h
Date: Wed, 16 Feb 2022 12:27:05 -0800	[thread overview]
Message-ID: <> (raw)

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

./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:

It also occurs in the Android codebase in multiple projects.

It has come up on the mailing list multiple times:

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))

                 reply	other threads:[~2022-02-16 20:27 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \ \ \ \ \

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

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).