Boost C++ Libraries: Ticket #13185: serialization of null pointer fails in optimized builds https://svn.boost.org/trac10/ticket/13185 <p> invoke in oserializer.hpp dereferences a pointer (UB for nullptrs), and then checks if it is null on the next line. The optimizer detects this UB and assumes the pointer cannot be null, and optimizes the whole if block out. This affected our code in production (serializing nullptrs was a corner case for us), so I assume it's affecting other folks. </p> <p> Here's a demo of clang trunk performing this optimization (earlier versions didn't perform this optimization): <a class="ext-link" href="https://godbolt.org/g/y6sbZP"><span class="icon">​</span>https://godbolt.org/g/y6sbZP</a> </p> <p> <a class="ext-link" href="https://github.com/boostorg/serialization/blob/6b33d1cd4e11daaf97612561ecd9d4848843897c/include/boost/archive/detail/oserializer.hpp#L468"><span class="icon">​</span>https://github.com/boostorg/serialization/blob/6b33d1cd4e11daaf97612561ecd9d4848843897c/include/boost/archive/detail/oserializer.hpp#L468</a> </p> <pre class="wiki"> template&lt;class TPtr&gt; static void invoke(Archive &amp;ar, const TPtr t){ register_type(ar, * t); if(NULL == t){ basic_oarchive &amp; boa = boost::serialization::smart_cast_reference&lt;basic_oarchive &amp;&gt;(ar); boa.save_null_pointer(); save_access::end_preamble(ar); return; } save(ar, * t); } </pre><p> Modifying the code to this, works as expected (although there's still a dereference of a null): </p> <pre class="wiki"> template&lt;class TPtr&gt; static void invoke(Archive &amp;ar, const TPtr t){ if(NULL == t){ register_type(ar, * t); basic_oarchive &amp; boa = boost::serialization::smart_cast_reference&lt;basic_oarchive &amp;&gt;(ar); boa.save_null_pointer(); save_access::end_preamble(ar); return; } else { register_type(ar, * t); } save(ar, * t); } </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/13185 Trac 1.4.3 anonymous Thu, 31 Aug 2017 23:21:39 GMT version, component changed; owner set https://svn.boost.org/trac10/ticket/13185#comment:1 https://svn.boost.org/trac10/ticket/13185#comment:1 <ul> <li><strong>owner</strong> set to <span class="trac-author">Robert Ramey</span> </li> <li><strong>version</strong> <span class="trac-field-old">Boost 1.63.0</span> → <span class="trac-field-new">Boost Development Trunk</span> </li> <li><strong>component</strong> <span class="trac-field-old">None</span> → <span class="trac-field-new">serialization</span> </li> </ul> Ticket