Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#13611 closed Bugs (invalid)

SEGFAULT when logging std::to_string(NAN)

Reported by: Filip Matzner Owned by: Andrey Semashev
Milestone: To Be Determined Component: log
Version: Boost 1.67.0 Severity: Problem
Keywords: Cc: boost@…

Description

Consider the following code:

#include <climits>
#include <boost/log/trivial.hpp>

int main()
{
    BOOST_LOG_TRIVIAL(info) << std::to_string(std::numeric_limits<double>::quiet_NaN());
}

When compiled with:

g++ -std=c++17 -DBOOST_ALL_DYN_LINK -pthread -lboost_log -O3 test.cpp && ./a.out

It crashes with SEGFAULT. If I replace BOOST_LOG_TRIVIAL(info) with std::cout, it does not.

System:

[floop@pine /tmp ]$ g++ -v
...
gcc version 8.1.1 20180531 (GCC)
[floop@pine /tmp ]$ uname -a
Linux pine 4.16.13-2-ARCH #1 SMP PREEMPT Fri Jun 1 18:46:11 UTC 2018 x86_64 GNU/Linux

Note that the SEGFAULT does not happen with clang++, but only g++. It also does not happen with -O2, but only -O3. It might also be a compiler bug, you will probably know better than me. :-)

Change History (3)

comment:1 by Andrey Semashev, 4 years ago

Resolution: invalid
Status: newclosed

In order to compile the test I had to modify the compiler command line:

g++ -g -std=c++17 -I. -L ./stage/lib -DBOOST_ALL_DYN_LINK -pthread -O3 log_nan_crash.cpp -lboost_log -lboost_thread -lboost_filesystem -lboost_system

gdb shows that the crash is happening during std::string construction:

#0  0x0000555555556221 in std::char_traits<char>::copy (__n=3, __s2=0x7fffffffd4e0 "nan", __s1=<optimized out>) at /usr/include/c++/7/bits/char_traits.h:350
#1  std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_S_copy (__n=3, __s=0x7fffffffd4e0 "nan", __d=<optimized out>) at /usr/include/c++/7/bits/basic_string.h:340
#2  std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_S_copy_chars (__k2=0x7fffffffd4e3 "", __k1=0x7fffffffd4e0 "nan", __p=<optimized out>) at /usr/include/c++/7/bits/basic_string.h:382
#3  std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct<char*> (__end=0x7fffffffd4e3 "", __beg=0x7fffffffd4e0 "nan", this=0x7fffffffd7d0) at /usr/include/c++/7/bits/basic_string.tcc:225
#4  std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct_aux<char*> (__end=0x7fffffffd4e3 "", __beg=0x7fffffffd4e0 "nan", this=0x7fffffffd7d0) at /usr/include/c++/7/bits/basic_string.h:236
#5  std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct<char*> (__end=0x7fffffffd4e3 "", __beg=0x7fffffffd4e0 "nan", this=0x7fffffffd7d0) at /usr/include/c++/7/bits/basic_string.h:255
#6  std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string<char*, void> (__a=..., __end=0x7fffffffd4e3 "", __beg=0x7fffffffd4e0 "nan", this=0x7fffffffd7d0) at /usr/include/c++/7/bits/basic_string.h:607
#7  __gnu_cxx::__to_xstring<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, char> (__convf=<optimized out>, __n=328, __fmt=0x5555555567d8 "%f", __fmt=0x5555555567d8 "%f", __n=328, __convf=<optimized out>)
    at /usr/include/c++/7/ext/string_conversions.h:115
#8  0x00005555555556f8 in std:: (__val=nan(0x8000000000000)) at /usr/include/c++/7/bits/basic_string.h:6450
#9  main () at log_nan_crash.cpp:6

This is definitely a compiler problem. Please, report to gcc devs: https://gcc.gnu.org/bugzilla/

comment:2 by Andrey Semashev, 4 years ago

BTW, I reproduced it with gcc 7.3 on Kubuntu 18.04.

Note: See TracTickets for help on using tickets.