Clang UBSAN 报告类型 'std::_Ios_Fmtflags' 的无效值

Clang++ UBSAN reports invalid value fro type 'std::_Ios_Fmtflags'

以下代码用clang UBSAN编译会崩溃

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>

template<class T>
inline std::string floatToString(T i){
    printf("in floatToString\n");
    std::stringstream ss;
    ss.precision(6);
    ss << std::noshowpoint << i;
    printf("exit floatToString\n");
    return ss.str();
}

int main() {
 std::cout << floatToString(1.0) << "\n";
 return 0;
}

使用 Clang 3.6 编译:

$> clang++-3.6 -fsanitize=undefined -fno-sanitize=float-divide-by-zero,vptr,function -fno-sanitize-recover -o test test.cpp

然后程序崩溃了:

$> ./test in floatToString /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/ios_base.h:96:24: runtime error: load of value 4294966271, which is not a valid value for type 'std::_Ios_Fmtflags'

有人可以帮助解释为什么当我打开 clang UBSAN 时会出现这个问题吗?

根据 http://lists.cs.uiuc.edu/pipermail/cfe-dev/2013-January/027401.html,这是 libstdc++ 中的错误。

仍然存在于 libstdc++ 5.1 中。

libc++ 按预期工作:

$ clang++ -stdlib=libc++ -fsanitize=undefined -fno-sanitize=float-divide-by-zero,vptr,function -fno-sanitize-recover -o test test.cpp -lc++abi

$ ./test
in floatToString
exit floatToString
1