chriskohlhoff / asio

Asio C++ Library
http://think-async.com/Asio
4.85k stars 1.21k forks source link

NULL deference exception in boost::asio::ip::tcp::resolver::results_type #608

Open ghost opened 3 years ago

ghost commented 3 years ago

@jackpoz commented on Apr 27, 2018, 7:53 PM UTC:

boost::asio::ip::tcp::resolver::results_type type triggers a NULL dereference exception when calling empty() on a default-initialized instance.

How to reproduce:

Code example:

#include <boost/asio/ip/tcp.hpp>

int main(int /*argc*/, char** /*argv*/)
{
  boost::asio::ip::tcp::resolver::results_type test = boost::asio::ip::tcp::resolver::results_type();
  if (test.empty())
    printf("ok");
}

Boost version: 1.66 Windows x64 vc141

Issue analysis: empty() is implemented as

bool empty() const BOOST_ASIO_NOEXCEPT
{
  return this->values_->empty();
}

but this->values returns NULL when the class is empty. A possible fix could be to NULL-check this->values and then call ->empty() (if still needed at all)

bool empty() const BOOST_ASIO_NOEXCEPT
{
  return !this->values_ || this->values_->empty();
}

Current workaround: I would suggest not to use .empty() at all currently but to rely on .begin() == .end() .

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.

This issue was moved by chriskohlhoff from boostorg/asio#101.

ghost commented 3 years ago

@jackpoz commented on May 24, 2018, 8:33 PM UTC:

is there something missing in my issue description that I could add to speed it up ?

ghost commented 3 years ago

@jackpoz commented on Oct 31, 2018, 10:07 AM UTC:

chriskohlhoff do you need any other information or do you want me to pull request the fix or do you have any comment at all about this issue (or the status/future of this project, for the matter) ?