From 6e66a5b77b1c3317f7fbdd3614b20ffda2649d7a Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 10 Feb 2015 22:46:57 +0100 Subject: net/Resolver: relax size check in LocalAddressToString() Handles abstract sockets on Linux. --- src/net/Resolver.cxx | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/net/Resolver.cxx b/src/net/Resolver.cxx index c7a684263..19df84096 100644 --- a/src/net/Resolver.cxx +++ b/src/net/Resolver.cxx @@ -23,6 +23,8 @@ #include "util/Error.hxx" #include "util/Domain.hxx" +#include + #ifndef WIN32 #include #include @@ -48,10 +50,26 @@ const Domain resolver_domain("resolver"); static std::string LocalAddressToString(const struct sockaddr_un &s_un, size_t size) { - if (size < sizeof(s_un) || s_un.sun_path[0] == 0) + const size_t prefix_size = (size_t) + ((struct sockaddr_un *)nullptr)->sun_path; + assert(size >= prefix_size); + + size_t result_length = size - prefix_size; + + /* remove the trailing null terminator */ + if (result_length > 0 && s_un.sun_path[result_length - 1] == 0) + --result_length; + + if (result_length == 0) return "local"; - return s_un.sun_path; + std::string result(s_un.sun_path, result_length); + + /* replace all null bytes with '@'; this also handles abstract + addresses (Linux specific) */ + std::replace(result.begin(), result.end(), '\0', '@'); + + return result; } #endif -- cgit v1.2.3