最近遇到了一个编译告警,严格别名和类型双关,整理一下相关的材料。

strict aliasing

以下问题摘自1

I have the following questions:

  1. If I leave the code with this warning, will it generate potentially wrong code?
  2. Is there any way to work around this problem?
  3. If there isn’t, is it possible to turn off strict aliasing from inside the source file (because I don’t want to turn it off for all source files and I don’t want to make a separate Makefile rule for this source file)?
    And yes, I actually need this kind of aliasing.

The strict aliasing rule makes this setup illegal, two unrelated types can’t point to the same memory. char * are defined as being able to alias anything, only char* / unsigned char* has this privilege.

aliasing assumptions

这个告警只有在 strict aliasing optimization 开启的情况下才会出现,gcc-O2 优化会开启上述优化2,因此会出现该告警,忽略该告警有可能产生错误代码。出现该告警的原因是编译器为了优化代码,做了一个假设,即指针不会有其他类型的别名, (char *) 例外,意思是两个类型不同的指针不会指向同一块内存。当然这个特性孰好孰坏我们这里暂不讨论,实际上争议还是比较大的3

Aliasing rules simply say that you can only access an object through its own type, its signed / unsigned variant type, or through a character type (signed char, unsigned char).

如何解决

如题主所问,如果我们确实需要做指针强制类型转换,怎样才能解决这个告警并得到健壮的代码呢?
C/C++ 是非常贴近硬件底层的语言,指针为我们编程提供了很大的便利性,同时也引入了一些隐患。我们可以很方便地重新解析一块内存上的数据(reinterpret) ,隐患是相伴便利性而生的,有时指针类型强转并不安全,例如出现未初始化字节数据的问题。

我们可以采用以下做法来消除这个告警:

  • 采用 __attribute__((__may_alias__)) 属性去提示编译器对某个变量关闭上面提到的假设。
  • 关闭这个假设, -fno-strict-aliasing,可能影响优化,linux kernel 有大量的指针操作,其构建带这个选项。
  • 屏蔽这个告警,-Wno-strict-aliasing,很不推荐!有掩耳盗铃之嫌!
  • 使用 union 来解决,这也是 gcc 推荐的做法。type punning via unions feels natural
int retLen;
someSetFunc((unsigned long*)&retLen);
printf("ret len = %d\n", retLen);...union u_retLen {int retLen;unsigned long ptr;
};
someSetFunc(&u_retLen.ptr);
printf("ret len = %d\n", u_retLen.retLen);

  1. https://stackoverflow.com/questions/4163126/dereferencing-type-punned-pointer-will-break-strict-aliasing-rules-warning/4163223 ↩︎

  2. https://gcc.gnu.org/onlinedocs/gcc-7.5.0/gcc/Optimize-Options.html#Optimize-Options ↩︎

  3. https://lkml.org/lkml/2018/6/5/769 ↩︎

dereferencing type-punned pointer will break strict-aliasing rules相关推荐

  1. Understanding C/C++ Strict Aliasing

    Understanding C/C++ Strict Aliasing 深入理解C/C++中的`Strict Aliasin`规则 or - Why won't the #$@##@^% compil ...

  2. GCC:dereferencing type-punned pointer will break strict-aliasing rules

    linux: 编译C,出现了dereferencing type-punned pointer will break strict-aliasing rules.即 取消对类型指针的引用将打破严格的别 ...

  3. GCC strict aliasing – 嫉妒就是承认自己不如别人

    GCC strict aliasing – 嫉妒就是承认自己不如别人 GCC strict aliasing 事情是这样的.我们对tair(淘宝的分布式Key/Value系统)动了一次大手术,更换了网 ...

  4. strict aliasing

    strict aliasing一文中将这些条文允许alias总结如下: 1.兼容类型(指相同类型?)或差别仅在于signed.unsigned.const.volatile的类型(比如 const u ...

  5. 【转载】升级OpenSSL修复高危漏洞Heartbleed

    背景:        OpenSSL全称为Secure Socket Layer,是Netscape所研发,利用数据加密(Encryption)作技术保障在Internet上数据传输的安全.可确保数据 ...

  6. g++的英文版使用说明和选项

    使用g++ -v --help可以列出g++的所有可选项 当然脚本最香了 g++ -v --help >> lionel.txt Usage: g++ [options] file... ...

  7. 【GCC】gcc警告选项汇总--编辑中|gcc编译选项

    目录 前言 请求或取消警告选项 GCC编译选项 参考原文:https://blog.csdn.net/qq_17308321/article/details/79979514 前言 警告:不是错误的, ...

  8. gcc警告选项汇总 转

    ---------------- 原文链接:https://blog.csdn.net/qq_17308321/article/details/79979514 参考资料:https://gcc.gn ...

  9. 严格的别名规则是什么?

    当询问C中常见的未定义行为时 ,人们有时会参考严格的别名规则. 他们在说什么? #1楼 正如Doug T.已经写过的附录一样,这是一个简单的测试用例,可能会用gcc触发它: 检查 #include & ...

最新文章

  1. 优化自动化测试流程,使用 flask 开发一个 toy jenkins工具
  2. OSPF协议工作原理
  3. 数据结构 - 单链表(百度面试题单链表的倒序打印)
  4. amd cpu不能在cmd环境下运行java代码_如何在Windows10中配置java的JDK环境
  5. 《Cortex-M0权威指南》之Cortex-M0编程入门
  6. mysql数据库:mysql增删改、单表、多表及子查询
  7. Java代码实现SM2算法以及注意点总结(踩坑记录)
  8. Adobe LiveCycle Designer 报表设计器
  9. 超像素池化全监督语义分割
  10. kali metsploit 工具入侵windows
  11. 串行外设接口(Serial Peripheral Interface, SPI)
  12. 如何优雅的(不花钱)获取一本技术图书
  13. 留几手是怎么火起来的?
  14. 软件测试薪资高不高?软件测试工资水平调研公布
  15. 是否开启谷歌Ad广告中的搜索网络合作伙伴?
  16. 如何配置 SSH 免密码登录
  17. 一种用几何解nonlinear ICA的方法
  18. 稳定的服务器主板,高稳定英特尔S2400SC2双路服务器主板
  19. SQL计算日环比、周同比
  20. 用友U8与聚水潭系统对接方案(聚水潭主管库存)

热门文章

  1. React 更新阶段的生命周期 componentWillReceiveProps->shouldComponentUpdate->componentWillUpdate
  2. 什么是真正的流程管理?流程管理的是与不是。
  3. 【JavaScript】【5】定时器(包含回调函数与Promise)
  4. Cpython和Jython的对比
  5. 7月02日13点,北京,「读书会」干货型毕业典礼
  6. pushmail的一点总结
  7. 神经网络碰上高斯过程,DeepMind论文开启深度学习新方向
  8. 服务器机柜防雷接地系统,数据中心机房防雷接地方法与措施实例分享
  9. Android Studio入门教程(计算器)
  10. 【数字识别】基于模板匹配实现OCR印刷字母+数字识别含Matlab源码