Search code examples
cgcctype-conversionsolarisgcc-warning

Mysterious type conversion warning when using sys/socket.h macro


I'm trying to resolve conversion warnings in our C code base on Solaris 11 64-bit using GCC 9.1 (iso9899:1999) and GNU make 4.2, and I came across this one:

warning: unsigned conversion from ‘int’ to ‘long unsigned int’ changes value from ‘-8’ to ‘18446744073709551608’ [-Wsign-conversion]
  187 |     char ccmsg[CMSG_SPACE(sizeof(int))];
      |                      ^~~~~~~~~~

I know that CMSG_SPACE is defined in sys/socket.h as:

/* Amount of space + padding needed for a message of length l */
#define CMSG_SPACE(l)                           \
    ((unsigned int)_CMSG_HDR_ALIGN(sizeof (struct cmsghdr) + (l)))

However, I am not understanding where the conversion is happening and how to resolve it. Google was of no help.

EDIT Here is some more info from the header file, as requested in the comments:

#if defined(__sparc)
/* To maintain backward compatibility, alignment needs to be 8 on sparc. */
#define _CMSG_HDR_ALIGNMENT 8
#else
/* for __amd64 (and other future architectures) */
#define _CMSG_HDR_ALIGNMENT 4
#endif  /* defined(__sparc) */

#define _CMSG_DATA_ALIGNMENT    (sizeof (int))
#define _CMSG_HDR_ALIGN(x)  (((uintptr_t)(x) + _CMSG_HDR_ALIGNMENT - 1) & \
                    ~(_CMSG_HDR_ALIGNMENT - 1))
#define _CMSG_DATA_ALIGN(x) (((uintptr_t)(x) + _CMSG_DATA_ALIGNMENT - 1) & \
                    ~(_CMSG_DATA_ALIGNMENT - 1))
#define CMSG_DATA(c)                            \
    ((unsigned char *)_CMSG_DATA_ALIGN((struct cmsghdr *)(c) + 1))

Solution

  • The -8 is coming from ~(_CMSG_HDR_ALIGNMENT - 1), and the long unsigned int conversion is coming from uintptr_t.

    The compiler is warning about conversion of -8 to uintptr_t before applying the & operator.

    NOTE: answer is from a comment left by @jxh