2015-06-29 22 views
3

下面的代碼將崩潰,當它們被編譯通過鐺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?

+0

參見[這是代碼真的不確定,因爲鏘似乎預示?](http://stackoverflow.com/q/30122500/1708801) –

回答

0

這是根據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 
+0

感謝。這現在起作用。 – zhanxw

+0

@zhanxw:我仍然遇到「運行時錯誤」:'/usr/lib64/gcc/x86_64-unknown-linux-gnu/5.2.0/../../../../include/c++/5.2 .0/bits/ios_base.h:102:24:運行時錯誤:加載值爲4294966271,這對於類型'std :: _ Ios_Fmtflags''不是有效值。 – Thomas

+0

您使用libC++並遇到錯誤嗎?我試過你的解決方案,它使用libC++而不是libstdC++。 – zhanxw