cpp-netlib/uri

Calling `authority` on URI with empty-string port crashes

Closed this issue · 2 comments

Setting the authority with an empty port and then getting that authority causes a crash.

auto uri = network::uri_builder()
    .authority("localhost:")
    .uri();
std::cout << *uri.authority() << '\n';

Root cause

The root cause is in the definition of the uri::authority() function.

boost::optional<boost::string_ref> uri::authority() const {
   auto host = this->host();
   if (!host) {
     return boost::optional<boost::string_ref>();
   }

   auto first = std::begin(*host), last = std::end(*host);
   auto user_info = this->user_info();
   if (user_info) {
     first = std::begin(*user_info);
   }

   auto port = this->port();
   if (port) {
     last = std::end(*port);
   }

   return to_string_ref(first, last);
 }

The local variable port will be initialized as the empty string referencing a default-initialized string_type, subsequently causing the last variable to be assigned a null value. That causes the range (first, last) to be invalid.

This issue may be closed.

Thanks.