mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Colin Cross <ccross@google.com>
To: musl@lists.openwall.com
Cc: Christian Eggers <ceggers@arri.de>, Colin Cross <ccross@google.com>
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: <20220216202705.2173894-1-ccross@google.com> (raw)

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


                 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:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

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

  git send-email \
    --in-reply-to=20220216202705.2173894-1-ccross@google.com \
    --to=ccross@google.com \
    --cc=ceggers@arri.de \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* 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

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