[闲聊] 关于C++的雷

楼主: PkmX (阿猫)   2017-09-25 22:14:26
听说最近大家在讨论雷,我目前觉得最雷的应该就是这个了:
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 /
作者: freef1y3 ( )   2017-09-25 23:19:00
推推
作者: james732 (好人超)   2017-09-25 23:20:00
同场加映我看不懂发生了什么事 Q_Q
作者: asilzheng (asil)   2017-09-25 23:49:00
never_called有被呼叫到???
作者: stucode   2017-09-25 23:51:00
同场加映是 null pointer dereference 的 UB 吧
作者: LPH66 (-6.2598534e+18f)   2017-09-25 23:56:00
我猜...因为 fp() 当 fp == NULL 时是 UB, 所以编译器假设写 fp() 时 fp 非空, 那非空时其值为何我猜就偷看其他函式
作者: KAOKAOKAO (鬼斗)   2017-09-26 00:09:00
同场加映参见:Why undefined behavior may call a never-called funcgoogle 就有
作者: chuegou (chuegou)   2017-09-26 00:14:00
老师他偷看!
作者: LPH66 (-6.2598534e+18f)   2017-09-26 01:28:00
所以我猜对了XD 果然是偷看之后最佳化掉了
作者: firejox (Tangent)   2017-09-26 03:39:00
推推
作者: Neisseria (Neisseria)   2017-09-26 04:34:00
原 po 很专业,但没事不会想这样写 XD
作者: stucode   2017-09-26 05:31:00
推推,感谢说明还有关键字。
作者: descent (“雄辩是银,沉默是金”)   2017-09-26 09:11:00
感谢分享clang++ -O2 根据什么把 loop 消去?
作者: shadow0326 (非议)   2017-09-26 11:03:00
长知识
作者: james732 (好人超)   2017-09-26 12:54:00
感谢说明,UB好可怕
作者: Sidney0503 (Sidney0503)   2017-09-26 18:12:00
这个有欢乐
作者: splasky (splasky)   2017-09-26 21:56:00
作者: VictorTom (鬼翼&娃娃鱼)   2017-09-27 02:59:00
雷爆了~~以前在UserMode塞while(true)等debugger欸.Orz
作者: st1009 (前端攻城师)   2017-09-27 22:28:00
加映执行是不是电脑会被删光阿?
作者: Lipraxde (Lipraxde)   2017-09-27 23:20:00
看到"rm -rf /"就怕怕 QwQ
作者: dou0228 (7777)   2017-09-28 09:04:00
同场加映的,平常就要养成习惯给初始值
作者: kdjf (我抓得到什么呢?)   2017-09-28 15:29:00
等debugger就不会开太大的OPT吧,应该没差(?)
作者: yvb   2017-09-28 19:16:00
至少 P 大很有良心地给 puts() 而非 system() 来加映.
作者: KoenigseggG (地表最速)   2017-09-28 20:08:00
但还是吓到人了XD
作者: dou0228 (7777)   2017-09-29 08:50:00
给 system() 就看看谁没事爱用root 登入囉

Links booklink

Contact Us: admin [ a t ] ucptt.com