Boost C++ Libraries: Ticket #13554: NULL deference exception in boost::asio::ip::tcp::resolver::results_type https://svn.boost.org/trac10/ticket/13554 <p> boost::asio::ip::tcp::resolver::results_type type triggers a NULL dereference exception when calling empty() on a default-initialized instance. </p> <p> How to reproduce: </p> <ul><li>instantiate a new boost::asio::ip::tcp::resolver::results_type class instance with the default parameterless constructor </li><li>call the empty() method, it should return true </li><li>notice the access violation crash triggered </li></ul><p> Code example: </p> <div class="wiki-code"><div class="code"><pre><span class="cp">#include</span> <span class="cpf">&lt;boost/asio/ip/tcp.hpp&gt;</span><span class="cp"></span> <span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">int</span> <span class="cm">/*argc*/</span><span class="p">,</span> <span class="kt">char</span><span class="o">**</span> <span class="cm">/*argv*/</span><span class="p">)</span> <span class="p">{</span> <span class="n">boost</span><span class="o">::</span><span class="n">asio</span><span class="o">::</span><span class="n">ip</span><span class="o">::</span><span class="n">tcp</span><span class="o">::</span><span class="n">resolver</span><span class="o">::</span><span class="n">results_type</span> <span class="n">test</span> <span class="o">=</span> <span class="n">boost</span><span class="o">::</span><span class="n">asio</span><span class="o">::</span><span class="n">ip</span><span class="o">::</span><span class="n">tcp</span><span class="o">::</span><span class="n">resolver</span><span class="o">::</span><span class="n">results_type</span><span class="p">();</span> <span class="k">if</span> <span class="p">(</span><span class="n">test</span><span class="p">.</span><span class="n">empty</span><span class="p">())</span> <span class="n">printf</span><span class="p">(</span><span class="s">&quot;ok&quot;</span><span class="p">);</span> <span class="p">}</span> </pre></div></div><p> Boost version: 1.66 Windows x64 vc141 </p> <p> Issue analysis: empty() is implemented as </p> <div class="wiki-code"><div class="code"><pre><span class="kt">bool</span> <span class="nf">empty</span><span class="p">()</span> <span class="k">const</span> <span class="n">BOOST_ASIO_NOEXCEPT</span> <span class="p">{</span> <span class="k">return</span> <span class="k">this</span><span class="o">-&gt;</span><span class="n">values_</span><span class="o">-&gt;</span><span class="n">empty</span><span class="p">();</span> <span class="p">}</span> </pre></div></div><p> but this-&gt;values_ returns NULL when the class is empty. A possible fix could be to NULL-check this-&gt;values_ and then call -&gt;empty() (if still needed at all) </p> <div class="wiki-code"><div class="code"><pre><span class="kt">bool</span> <span class="nf">empty</span><span class="p">()</span> <span class="k">const</span> <span class="n">BOOST_ASIO_NOEXCEPT</span> <span class="p">{</span> <span class="k">return</span> <span class="o">!</span><span class="k">this</span><span class="o">-&gt;</span><span class="n">values_</span> <span class="o">||</span> <span class="k">this</span><span class="o">-&gt;</span><span class="n">values_</span><span class="o">-&gt;</span><span class="n">empty</span><span class="p">();</span> <span class="p">}</span> </pre></div></div><p> Current workaround: I would suggest not to use .empty() at all currently but to rely on .begin() == .end() . </p> <p> Note that even if the code example above looks quite meaningless, .empty() crashes causes issue for example when checking if an address can be resolved using ip::basic_resolver::resolve function. The documentation states "An empty range is returned if an error occurs" so one expects to be able to check if the range is empty. </p> <p> P.S.: I had links to sources and documentation but the tracker kept saying "Akismet says content is spam". Please fix your tracker. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/13554 Trac 1.4.3