When getting the connection info getnameinfo is called getting the
hostname and servname except when the sockaddr is a pure ipv6
address. In that last case only hostname is requested. Since servname
is stack allocated and not initialized it might contain garbage which
is then put in the log. Just always request both hostname and servname
with NI_NUMERICHOST | NI_NUMERICSERV.

Signed-off-by: Mark Wielaard <m...@klomp.org>
---
 debuginfod/ChangeLog      |  4 ++++
 debuginfod/debuginfod.cxx | 15 ++++++++++-----
 2 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/debuginfod/ChangeLog b/debuginfod/ChangeLog
index 983fd44a..257ac39f 100644
--- a/debuginfod/ChangeLog
+++ b/debuginfod/ChangeLog
@@ -1,3 +1,7 @@
+2022-05-09  Mark Wielaard  <m...@klomp.org>
+
+       * debuginfod.cxx (conninfo): Always provide servname to getnameinfo.
+
 2022-05-09  Mark Wielaard  <m...@klomp.org>
 
        * debuginfod-client.c (debuginfod_query_server): Add
diff --git a/debuginfod/debuginfod.cxx b/debuginfod/debuginfod.cxx
index 4aaf41c0..b664b74e 100644
--- a/debuginfod/debuginfod.cxx
+++ b/debuginfod/debuginfod.cxx
@@ -1060,8 +1060,10 @@ conninfo (struct MHD_Connection * conn)
   struct sockaddr *so = u ? u->client_addr : 0;
 
   if (so && so->sa_family == AF_INET) {
-    sts = getnameinfo (so, sizeof (struct sockaddr_in), hostname, sizeof 
(hostname), servname,
-                       sizeof (servname), NI_NUMERICHOST | NI_NUMERICSERV);
+    sts = getnameinfo (so, sizeof (struct sockaddr_in),
+                       hostname, sizeof (hostname),
+                       servname, sizeof (servname),
+                       NI_NUMERICHOST | NI_NUMERICSERV);
   } else if (so && so->sa_family == AF_INET6) {
     struct sockaddr_in6* addr6 = (struct sockaddr_in6*) so;
     if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
@@ -1071,11 +1073,14 @@ conninfo (struct MHD_Connection * conn)
       addr4.sin_port = addr6->sin6_port;
       memcpy (&addr4.sin_addr.s_addr, addr6->sin6_addr.s6_addr+12, 
sizeof(addr4.sin_addr.s_addr));
       sts = getnameinfo ((struct sockaddr*) &addr4, sizeof (addr4),
-                         hostname, sizeof (hostname), servname, sizeof 
(servname),
+                         hostname, sizeof (hostname),
+                         servname, sizeof (servname),
                          NI_NUMERICHOST | NI_NUMERICSERV);
     } else {
-      sts = getnameinfo (so, sizeof (struct sockaddr_in6), hostname, sizeof 
(hostname), NULL, 0,
-                         NI_NUMERICHOST);
+      sts = getnameinfo (so, sizeof (struct sockaddr_in6),
+                         hostname, sizeof (hostname),
+                         servname, sizeof (servname),
+                         NI_NUMERICHOST | NI_NUMERICSERV);
     }
   }
   
-- 
2.30.2

Reply via email to