【发布时间】:2015-05-08 11:16:48
【问题描述】:
我在使用单元测试库 Catch 的项目中打开了-fsanitize=undefined。来自 Catch 的一行被此标志指示为导致未定义的行为。我设法做了一个孤立的例子:
#include <iomanip>
#include <sstream>
int main()
{
std::ostringstream os;
os << "0x" << std::setfill('0') << std::hex;
}
编译:
clang++ -fsanitize=undefined main.cpp
如果我运行它,将给出以下打印:
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/ios_base.h:96:24: runtime error: load of value 4294967221, which is not a valid value for type 'std::_Ios_Fmtflags'
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/ios_base.h:76:67: runtime error: load of value 4294967221, which is not a valid value for type 'std::_Ios_Fmtflags'
这发生在我的 clang 3.6.0 和一个使用 clang 3.4-1ubuntu3 的朋友身上。我在 gcc 版本4.9.2 上不会发生这种情况@
那么这里是什么?这段代码真的很糟糕,还是在 clang 的末尾有什么可疑的地方?
【问题讨论】:
-
Just
os << std::hex;似乎也重现了这个问题。 -
我想要一个更具描述性的标题,但我很难想出一个。
标签: c++ clang undefined-behavior libstdc++ sanitizer