C_and_CPP 板


LINE

听说最近大家在讨论雷,我目前觉得最雷的应该就是这个了: int main() { while (true); // UB in C++11 or later } 是的,infinite loop 在 C++11 是 undefined behavior: 4.7.2 [intro.progress] The implementation may assume that any thread will eventually do one of the following: — terminate, — make a call to a library I/O function, — perform an access through a volatile glvalue, or — perform a synchronization operation or an atomic operation. ( 注:当标准说你可以 assume P 的时候,言下之意就是 not P 是 UB ) 很明显上面的 loop 不属於这四个的其中一个 ============================================================================== 於是国外就有无聊人士(?)造了一个例子用穷举法找费式最後定理的反例, 理论上当然是找不到的,所以该 loop 应该是个无穷回圈: #include <cstdint> #include <iostream> bool fermat() { constexpr int32_t MAX = 1000; int32_t a = 1, b = 1, c = 1; // Infinite loop while (true) { if (((a*a*a) == ((b*b*b)+(c*c*c)))) return false; ++a; if (a > MAX) { a=1; ++b; } if (b > MAX) { b=1; ++c; } if (c > MAX) { c=1; } } return true; } int main() { if (!fermat()) std::cout << "Fermat's Last Theorem has been disproved."; else std::cout << "Fermat's Last Theorem has not been disproved."; std::cout << std::endl; } $ clang++ -O2 test.cpp && ./a.out Fermat's Last Theorem has been disproved. Oops. ( 如果用 -O0 或是 GCC 的确是会进入无穷回圈 ) ============================================================================== 那在 C 底下的行为是怎麽样呢?C11 的标准如此说: 6.8.5 Iteration statements 6 An iteration statement whose controlling expression is not a constant expression (156) that performs no input/output operations, does not access volatile objects, and performs no synchronization or atomic operations in its body, controlling expression, or (in the case of for statement) its expression-3, may be assumed by the implementation to terminate. (157) 157) This is intended to allow compiler transformations such as removal of empty loops even when termination cannot be proven while(1); 的 1 刚好是一个 constant expression ,所以这条不适用, 但是稍微修改一下变成 while(1,1); 多了 comma op 就不是 constant expression 了, 这样的话 compiler 的确是可以把 while(1,1); 拿掉的 ============================================================================== 同场加映,踩到 UB 的下场: #include <stdio.h> static void (*fp)(void); void kerker(void) { puts("rm -rf /"); } void never_called(void) { fp = kerker; } int main(void) { fp(); return 0; } $ clang test.c -O2 && ./a.out rm -rf / --



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 140.113.193.217
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1506348868.A.0C3.html
1F:推 freef1y3: 推推 09/25 23:19
2F:→ james732: 同场加映我看不懂发生了什麽事 Q_Q 09/25 23:20
3F:→ asilzheng: never_called有被呼叫到??? 09/25 23:49
4F:推 stucode: 同场加映是 null pointer dereference 的 UB 吧 09/25 23:51
5F:推 LPH66: 我猜...因为 fp() 当 fp == NULL 时是 UB, 所以编译器假设 09/25 23:56
6F:→ LPH66: 写 fp() 时 fp 非空, 那非空时其值为何我猜就偷看其他函式 09/25 23:57
当然也要刚好 fp 是 static 且它的 address 没有被 escape 到 TU 之外, 注解里面的写法都会让这个「最佳化」失效:https://godbolt.org/g/1uwjBQ 因为 fp 可能的值只有 { NULL, kerker },而呼叫 NULL 是 UB, 所以编译器可以直接认定 fp 一定是 kerker 然後直接 inline 进去
7F:推 KAOKAOKAO: 同场加映参见: 09/26 00:09
8F:→ KAOKAOKAO: Why undefined behavior may call a never-called func 09/26 00:09
9F:→ KAOKAOKAO: google 就有 09/26 00:09
10F:推 chuegou: 老师他偷看! 09/26 00:14
11F:推 Caesar08: 09/26 01:15
※ 编辑: PkmX (140.113.193.217), 09/26/2017 01:31:30
12F:推 LPH66: 所以我猜对了XD 果然是偷看之後最佳化掉了 09/26 01:28
13F:推 firejox: 推推 09/26 03:39
14F:推 Neisseria: 原 po 很专业,但没事不会想这样写 XD 09/26 04:34
15F:推 stucode: 推推,感谢说明还有关键字。 09/26 05:31
16F:推 descent: 感谢分享 09/26 09:11
17F:推 descent: clang++ -O2 根据什麽把 loop 消去? 09/26 10:27
18F:推 shadow0326: 长知识 09/26 11:03
19F:推 james732: 感谢说明,UB好可怕 09/26 12:54
20F:推 Sidney0503: 这个有欢乐 09/26 18:12
21F:推 splasky: 推 09/26 21:56
22F:推 VictorTom: 雷爆了~~以前在UserMode塞while(true)等debugger欸.Orz 09/27 02:59
23F:推 st1009: 加映执行是不是电脑会被删光阿? 09/27 22:28
24F:推 Lipraxde: 看到"rm -rf /"就怕怕 QwQ 09/27 23:20
25F:→ dou0228: 同场加映的,平常就要养成习惯给初始值 09/28 09:04
26F:→ kdjf: 等debugger就不会开太大的OPT吧,应该没差(?) 09/28 15:29
27F:推 yvb: 至少 P 大很有良心地给 puts() 而非 system() 来加映. 09/28 19:16
28F:推 KoenigseggG: 但还是吓到人了XD 09/28 20:08
29F:推 dou0228: 给 system() 就看看谁没事爱用root 登入罗 09/29 08:50







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:iOS站内搜寻

TOP