使用sys/socket.h宏时的神秘类型转换警告 [英] Mysterious type conversion warning when using sys/socket.h macro

查看:111
本文介绍了使用sys/socket.h宏时的神秘类型转换警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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))];
      |                      ^~~~~~~~~~

我知道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没有帮助.

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

推荐答案

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

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

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

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

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

NOTE: answer is from a comment left by @jxh

这篇关于使用sys/socket.h宏时的神秘类型转换警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆