Boost C++ Libraries: Ticket #13611: SEGFAULT when logging std::to_string(NAN) https://svn.boost.org/trac10/ticket/13611 <p> Consider the following code: </p> <pre class="wiki">#include &lt;climits&gt; #include &lt;boost/log/trivial.hpp&gt; int main() { BOOST_LOG_TRIVIAL(info) &lt;&lt; std::to_string(std::numeric_limits&lt;double&gt;::quiet_NaN()); } </pre><p> When compiled with: </p> <pre class="wiki">g++ -std=c++17 -DBOOST_ALL_DYN_LINK -pthread -lboost_log -O3 test.cpp &amp;&amp; ./a.out </pre><p> It crashes with SEGFAULT. If I replace BOOST_LOG_TRIVIAL(info) with std::cout, it does not. </p> <p> System: </p> <pre class="wiki">[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 </pre><p> 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. :-) </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/13611 Trac 1.4.3 Andrey Semashev Wed, 20 Jun 2018 08:03:53 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/13611#comment:1 https://svn.boost.org/trac10/ticket/13611#comment:1 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">invalid</span> </li> </ul> <p> In order to compile the test I had to modify the compiler command line: </p> <pre class="wiki">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 </pre><p> gdb shows that the crash is happening during <code>std::string</code> construction: </p> <pre class="wiki">#0 0x0000555555556221 in std::char_traits&lt;char&gt;::copy (__n=3, __s2=0x7fffffffd4e0 "nan", __s1=&lt;optimized out&gt;) at /usr/include/c++/7/bits/char_traits.h:350 #1 std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::_S_copy (__n=3, __s=0x7fffffffd4e0 "nan", __d=&lt;optimized out&gt;) at /usr/include/c++/7/bits/basic_string.h:340 #2 std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::_S_copy_chars (__k2=0x7fffffffd4e3 "", __k1=0x7fffffffd4e0 "nan", __p=&lt;optimized out&gt;) at /usr/include/c++/7/bits/basic_string.h:382 #3 std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::_M_construct&lt;char*&gt; (__end=0x7fffffffd4e3 "", __beg=0x7fffffffd4e0 "nan", this=0x7fffffffd7d0) at /usr/include/c++/7/bits/basic_string.tcc:225 #4 std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::_M_construct_aux&lt;char*&gt; (__end=0x7fffffffd4e3 "", __beg=0x7fffffffd4e0 "nan", this=0x7fffffffd7d0) at /usr/include/c++/7/bits/basic_string.h:236 #5 std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::_M_construct&lt;char*&gt; (__end=0x7fffffffd4e3 "", __beg=0x7fffffffd4e0 "nan", this=0x7fffffffd7d0) at /usr/include/c++/7/bits/basic_string.h:255 #6 std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::basic_string&lt;char*, void&gt; (__a=..., __end=0x7fffffffd4e3 "", __beg=0x7fffffffd4e0 "nan", this=0x7fffffffd7d0) at /usr/include/c++/7/bits/basic_string.h:607 #7 __gnu_cxx::__to_xstring&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;, char&gt; (__convf=&lt;optimized out&gt;, __n=328, __fmt=0x5555555567d8 "%f", __fmt=0x5555555567d8 "%f", __n=328, __convf=&lt;optimized out&gt;) 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 </pre><p> This is definitely a compiler problem. Please, report to gcc devs: <a class="ext-link" href="https://gcc.gnu.org/bugzilla/"><span class="icon">​</span>https://gcc.gnu.org/bugzilla/</a> </p> Ticket Andrey Semashev Wed, 20 Jun 2018 08:04:57 GMT <link>https://svn.boost.org/trac10/ticket/13611#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/13611#comment:2</guid> <description> <p> BTW, I reproduced it with gcc 7.3 on Kubuntu 18.04. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Thu, 21 Jun 2018 22:27:24 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/13611#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/13611#comment:3</guid> <description> <p> Ok, thanks, reported here: <a class="ext-link" href="https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86274"><span class="icon">​</span>https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86274</a> </p> </description> <category>Ticket</category> </item> </channel> </rss>