g++的分段错误(在NaN上使用to_string两次时)



假设我们有以下设置:

# CMakeLists.txt
project(Scripts CXX)
add_executable(${PROJECT_NAME} script.cpp)
// script.cpp
#include <limits>
#include <string>
void func()
{
const double x = std::numeric_limits<double>::quiet_NaN();
std::to_string(x);
std::to_string(x);
}
int main()
{
func();
return 0;
}

当我们编译&在Linux上使用GNU7.3.0Release模式下运行此程序,我们会得到一个segfault:

-- The CXX compiler identification is GNU 7.3.0
-- Check for working CXX compiler: .../gcc-7.3.0/bin/g++
-- Check for working CXX compiler: .../gcc-7.3.0/bin/g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
====================[ Build | Scripts | Release ]===============================
.../clion-2020.1.1/bin/cmake/linux/bin/cmake --build .../test_scripts/cmake-build-release --target Scripts --
Scanning dependencies of target Scripts
[ 50%] Building CXX object CMakeFiles/Scripts.dir/script.cpp.o
[100%] Linking CXX executable Scripts
[100%] Built target Scripts
.../test_scripts/cmake-build-release/Scripts
Signal: SIGSEGV (Segmentation fault)

当注释掉第二个to_string(即//std::to_string(x);(或当将x更改为非nan值(即x = 10;(或在Debug模式下运行时,则不存在segfault。

是我做错了什么,还是这是一个g++错误?

这是g++错误吗?

是的,这是7.x版本中的一个错误:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86274

它在gcc的版本8中得到了修复。

相关内容

  • 没有找到相关文章

最新更新