使用sys / socket.h宏时的神秘类型转换警告

问题描述 投票:1回答:1

[我正在尝试使用GCC 9.1(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)))

但是,我不知道转换发生在哪里以及如何解决。 Google没有帮助。

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

#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))
c gcc type-conversion solaris gcc-warning
1个回答
0
投票

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

编译器在应用-8运算符之前,警告有关uintptr_t&的转换。

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

© www.soinside.com 2019 - 2024. All rights reserved.