Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Tue, 11 Oct 2016 16:45:40 +0200
From: Markus Wichmann <nullplan@....net>
To: musl@...ts.openwall.com
Subject: Re: Using macro CMSG_NXTHDR generates warnings with CLANG

On Mon, Oct 10, 2016 at 10:09:38PM +0000, Jan Vorlicek wrote:
> Trying to build a piece of code that uses CMSG_NXTHDR macro using CLANG (tested with CLANG 3.8) with all warnings enabled using -Weverything generates the following warnings:
> 
> clang++ -Weverything ./nettest.cpp -c -o nettest.o
> 
> ./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.
> 
> The testing source is below:
> 
> #include <sys/socket.h>
> cmsghdr* GET_CMSG_NXTHDR(msghdr* mhdr, cmsghdr* cmsg);
> 
> cmsghdr* GET_CMSG_NXTHDR(msghdr* mhdr, cmsghdr* cmsg)
> {
>     return CMSG_NXTHDR(mhdr, cmsg);
> }
> 
> Would it be possible to fix it so that no warnings are generated? We are building our application with -Weverything and currently we need to disable these two warnings around the CMSG_NXTHDR macro invocation.
> Thank you in advance for considering that!
> 
> Jan

Both of these appear to be unavoidable if the computation is to be done
inline, as it is at the moment. I wondered how glibc does it and they
just have a function (__cmsg_nxthdr()) taking care of it. I doubt most
contributors here would like to add a function with the sole purpose of
calculating a macro, just to shut up the compiler.

The first warning is unavoidable because the next control message header
can only be reached by skipping over the entire control message, the
length of which is recorded in bytes. We do take care of the alignment
doing that, but apparently clang is incapable of recognizing that.

The second one could be avoided with an explicit type conversion.

However, isn't our stance that warnings in system headers are broken,
anyway? And warnings resulting from expansions of macros out of system
headers just the same.

Ciao,
Markus

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.