Skip to content

Commit c6a6bef

Browse files
committed
fix(toolbox): Correctly print unix sockets with no path
Correctly print unix sockets with no path. SDB-10742
1 parent d2cc946 commit c6a6bef

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

toolbox/net/Endpoint.hpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,16 @@ template <typename StreamT, typename T>
7070
StreamT& print_unix_endpoint(StreamT& os, const T& ep)
7171
{
7272
constexpr const char* scheme = "unix://";
73-
// abstract unix socket's path starts with '\0' and not null-terminated
7473
const auto* path = reinterpret_cast<const sockaddr_un*>(ep.data())->sun_path;
7574
if (path[0] == '\0') {
76-
size_t size = ep.size() - sizeof(std::declval<sockaddr_un>().sun_family) - 1;
77-
os << scheme << '|' << std::string_view{path + 1, size};
75+
int size = ep.size() - sizeof(std::declval<sockaddr_un>().sun_family) - 1;
76+
if (size <= 0) {
77+
// No path
78+
os << scheme;
79+
return os;
80+
}
81+
// abstract unix socket's path starts with '\0' and not null-terminated
82+
os << scheme << '|' << std::string_view{path + 1, static_cast<std::size_t>(size)};
7883
return os;
7984
}
8085
os << scheme << *ep.data();

toolbox/net/Endpoint.ut.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,18 @@ BOOST_AUTO_TEST_CASE(ParseStreamUnixAbstractCase)
179179
BOOST_CHECK_EQUAL(to_string(ep), uri);
180180
}
181181

182+
BOOST_AUTO_TEST_CASE(ParseStreamUnixEmptyCase)
183+
{
184+
auto sock = os::socket(net::UnixStreamProtocol{});
185+
StreamEndpoint ep;
186+
get_sock_name(sock.get(), ep);
187+
188+
BOOST_CHECK_EQUAL(ep.protocol().family(), AF_UNIX);
189+
BOOST_CHECK_EQUAL(ep.protocol().type(), SOCK_STREAM);
190+
BOOST_CHECK_EQUAL(ep.protocol().protocol(), 0);
191+
BOOST_CHECK_EQUAL(to_string(ep), "unix://");
192+
}
193+
182194
BOOST_AUTO_TEST_CASE(ParseStreamBindCase)
183195
{
184196
const auto ep = parse_stream_endpoint("tcp4://:80");

0 commit comments

Comments
 (0)