C语言 使用 sys/socket.h 宏时出现神秘类型转换警告



我正在尝试使用 GCC 11 (iso9899:1999( 和 GNU make 4.2 解决 Solaris 11 64 位上的 C 代码库中的转换警告,我遇到了这个:

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

我知道CMSG_SPACEsys/socket.h中被定义为:

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

但是,我不明白转换在哪里发生以及如何解决它。谷歌没有帮助。

编辑这是头文件中的更多信息,如注释中的要求:

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

来自~(_CMSG_HDR_ALIGNMENT - 1)long unsigned int转换来自uintptr_t

编译器在应用&运算符之前警告将-8转换为uintptr_t

注意:答案来自@jxh留下的评论

最新更新