Warning: __attribute__((const)) does not mean function purity

栏目: IT技术 · 发布时间: 6年前

内容简介:Clang and GCC (and others) support function attributesTheConsider:

Clang and GCC (and others) support function attributes __attribute__((const)) and __attribute__((pure)) . These had always been explained to me thus:

  • __attribute__((pure)) : The function may read, but doesn’t modify any global state. [This is true!]
  • __attribute__((const)) : The function neither reads nor modifies global state. [ This is MASSIVELY OVERSIMPLIFIED to the point of being a lie!]

The gigantic asterisk on that simplified explanation of __attribute__((const)) is that pointers and references—even const pointers and references!—are disallowed if the memory they reference can change between successive invocations of the function.

Consider:

bool ends_with(const string & full, const string & suffix) __attribute__((const));

The implementation of this should certainly meet the requirements of neither reading nor writing global state… unless you consider the memory referenced by your parameters to be global state!

But that’s exactly what GCC’s docs on the attribute do consider as global state:

Note that a function that has pointer arguments and examines the data pointed to must not be declared  const if the pointed-to data might change between successive invocations of the function. In general, since a function cannot distinguish data that might change from data that cannot, const functions should never take pointer or, in C++, reference arguments.

(Note that Clang, despite having supported the attribute for years, has zero docs on it. Based on experience, though, it seems to have to the same restriction.)

The consequence of violating this requirement is that in the following code, result2 might wind up being true (if you get unlucky with the optimizer):

string s = "foo";
bool result1 = ends_with(s, "oo"s); // True, always
s = "bar";
bool result2 = ends_with(s, "oo"s); // True, maybe!

That’s a somewhat contrived example, but it can be much more insidious when it’s a member variable you’re passing to your __attribute__((const)) function. This can also bite you when you have implicit conversions to const ref (e.g., when you’re passing a string literal to a function that converts it to a const string reference).

The worst part of all this—and the reason I’ve spent 2 full days over the course of the last few months debugging this—is that neither the compiler nor UBsan provide any warnings that this is happening.

If you squint really hard, you can kind of see what the compiler implementers were thinking—what you’re “really” passing here is just a pointer, which (again if you squint hard enough) is really just an integer… it’s your fault, silly programmer, for turning looking at the (“global”) memory state corresponding to that int.

This is what people mean when they say C++ is a language of footguns.


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

零边际成本社会

零边际成本社会

[美]杰里米·里夫金 / 赛迪研究院专家组 / 中信出版社 / 2014-11-1 / 49.00

在这本书中,《第三次工业革命》作者杰里米•里夫金开创性地探讨了极致生产力、协同共享、产消者、生物圈生活方式等全新的概念,详细地描述了数以百万计的人生产和生活模式的转变。他认为,“产消者”正在以近乎零成本的方式制作并分享自己的信息、娱乐、绿色能源和3D打印产品。他们也通过社交媒体、租赁商、合作组织以极低或零成本的模式分享汽车、住房、服装和其他物品;学生更多地参与到基于零成本模式的开放式网络课程…… ......一起来看看 《零边际成本社会》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具