下面的代碼將崩潰,當它們被編譯通過鐺UBSAN鏘++ UBSAN報告無效值回回型 '的std :: _ Ios_Fmtflags'
#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;
}
編譯鏘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'
有人可以幫助爲什麼這個問題發生時,我打開鐺UBSAN?
參見[這是代碼真的不確定,因爲鏘似乎預示?](http://stackoverflow.com/q/30122500/1708801) –