From d15747db32d10f79e3cb3c6bb64548f1491a8c3c Mon Sep 17 00:00:00 2001 From: dmiller Date: Thu, 22 Jul 2021 17:18:29 +0000 Subject: [PATCH] Minor optimization for name resolution: null servname --- libnetutil/netutil.cc | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/libnetutil/netutil.cc b/libnetutil/netutil.cc index 1f61df4bc..b1f8241bc 100644 --- a/libnetutil/netutil.cc +++ b/libnetutil/netutil.cc @@ -358,6 +358,7 @@ static int resolve_internal(const char *hostname, unsigned short port, struct addrinfo hints; struct addrinfo *result; char portbuf[16]; + char *servname = NULL; int rc; assert(hostname); @@ -370,10 +371,13 @@ static int resolve_internal(const char *hostname, unsigned short port, hints.ai_flags |= addl_flags; /* Make the port number a string to give to getaddrinfo. */ - rc = Snprintf(portbuf, sizeof(portbuf), "%hu", port); - assert(rc >= 0 && (size_t) rc < sizeof(portbuf)); + if (port != 0) { + rc = Snprintf(portbuf, sizeof(portbuf), "%hu", port); + assert(rc >= 0 && (size_t) rc < sizeof(portbuf)); + servname = portbuf; + } - rc = getaddrinfo(hostname, portbuf, &hints, &result); + rc = getaddrinfo(hostname, servname, &hints, &result); if (rc != 0) return rc; if (result == NULL)