With g++ version gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~16.04) , when I compile this code like g++ -O3 <file>, and run the executable, then the app gives a seg fault
#include <iostream>
#include <cmath>
#include <string>
void test() {
std::cout << "test: " << std::endl;
std::cout << "init" << std::endl;
std::cout << "inf" << std::endl;
std::cout << std::to_string(INFINITY) << std::endl;
std::cout << "init done" << std::endl;
}
int main ()
{
std::cout << "test: " << std::endl;
std::cout << "init" << std::endl;
std::cout << "inf" << std::endl;
std::cout << std::to_string(INFINITY) << std::endl;
std::cout << "init done" << std::endl;
return 0;
}
~
The seg fault call stack is below:
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400f01 in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > __gnu_cxx::__to_xstring<std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> >, char>(int (*)(char*, unsigned long, char const*, __va_list_tag*), unsigned long, char const*, ...) [clone .constprop.20] ()
(gdb) bt
#0 0x0000000000400f01 in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > __gnu_cxx::__to_xstring<std::__cxx11::basic_string<char, std::char_traits<cha
r>, std::allocator<char> >, char>(int (*)(char*, unsigned long, char const*, __va_list_tag*), unsigned long, char const*, ...) [clone .constprop.20] ()
#1 0x0000000000400c63 in main ()
Interestingly, when I comment test function, then it does not happen.. Not sure if it is a case of memory corruption
Other facts
-O3Live demo
This is an old gcc bug and also noticed that SEGFAULT does not happen with -O2, but only -O3. This was fixed/patched in the later versions.
[7 Regression] SEGFAULT when logging std::to_string(NAN)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With