【问题标题】:Mysterious type conversion warning when using sys/socket.h macro使用 sys/socket.h 宏时出现神秘的类型转换警告
【发布时间】:2023-03-21 04:57:01
【问题描述】:

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

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

编辑 根据 cmets 的要求,这是来自头文件的更多信息:

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

【问题讨论】:

  • _CMSG_HDR_ALIGN 是如何定义的?
  • 您可以使用-E 调用GCC 以查看扩展源代码的样子。这应该会给你一些提示。
  • -8 来自~(_CMSG_HDR_ALIGNMENT - 1)long unsigned int 转换来自uintptr_t。在应用 & 运算符之前,编译器会警告 -8uintptr_t 的转换。
  • 这似乎是一个虚假的警告,充其量。 -8unsigned long int 的转换被 6.3.1.3 Signed and unsigned integers, paragraph 2 of the C standard 明确覆盖:“否则,如果新类型是无符号的,则在新类型中可以表示的最大值之外重复加减一类型,直到值在新类型的范围内。”

标签: c gcc type-conversion solaris gcc-warning


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-28
    相关资源
    最近更新 更多