Reputation: 3299
The following codes will crash when they are compiled by 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;
}
Compile with Clang 3.6:
$> clang++-3.6 -fsanitize=undefined -fno-sanitize=float-divide-by-zero,vptr,function -fno-sanitize-recover -o test test.cpp
Then the program crashed:
$> ./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'
Can someone help why this problem happens when I turn on clang UBSAN?
Upvotes: 4
Views: 540
Reputation: 3225
It's a bug in libstdc++ according to http://lists.cs.uiuc.edu/pipermail/cfe-dev/2013-January/027401.html.
Still present in libstdc++ 5.1.
libc++ works as expected:
$ 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
Upvotes: 1