Clang ++ UBSAN reports invalid value of type type 'std :: _ Ios_Fmtflags'
The following codes will fail when 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;
}
Compiling 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 value 4294966271, which is not a valid value for type 'std :: _ Ios_Fmtflags'
Can anyone please help why this problem occurs when I enable clang UBSAN?
+3
source to share
1 answer
This is 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
0
source to share