c++ `std::to_string(INFINITY)`使用`g++ -O3`给出seg错误`

zz2j4svz  于 2024-01-09  发布在  其他
关注(0)|答案(1)|浏览(99)

对于g++版本gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~16.04),当我像g++ -O3 <file>一样编译此代码并运行可执行文件时,应用程序会给出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;                                                                                                                                                                            
}                                                                                                                                                                                      
~

字符串
seg fault调用堆栈如下:

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 ()


有趣的是,当我注解test函数时,它不会发生。.不确定这是否是内存损坏的情况
其他事实

*如果我不使用-O3,则不会发生此情况

Live demo

blmhpbnm

blmhpbnm1#

这是一个旧的gcc bug,也注意到SEGFAULT不会发生在-O2上,而只发生在-O3上。这是后来版本中的fixed/patched
[7 Regression] SEGFAULT when logging std::to_string(NAN)

相关问题