Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`std::to_string(INFINITY)` gives seg fault with `g++ -O3`

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

  • This does not happen if I do not use -O3

Live demo

like image 484
cryptickey Avatar asked Nov 28 '25 17:11

cryptickey


1 Answers

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)

like image 116
Anoop Rana Avatar answered Dec 01 '25 07:12

Anoop Rana