daemon: fix IPv6 address corruption in lookup_hostname()

getaddrinfo() is called with AF_UNSPEC hints, so it may return IPv6 results. However, the code unconditionally casts ai_addr to sockaddr_in and passes AF_INET to inet_ntop(). On IPv6-only hosts, this reads from the wrong struct offset, producing garbage IP addresses. Fix this by checking ai_family and extracting the address pointer into a local variable before calling inet_ntop() once with the correct family. Die on unexpected address families. Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Sebastien Tardif committed May 28, 2026 at 02:56 UTC b8cda126b4e0fbfd514b26dec4ee8a1c6849abe9
1 file changed +13 -2
daemon.c
+13 -2
@@ -674,9 +674,20 @@ static void lookup_hostname(struct hostinfo *hi)
674
675 gai = getaddrinfo(hi->hostname.buf, NULL, &hints, &ai);
676 if (!gai) {
677 - struct sockaddr_in *sin_addr = (void *)ai->ai_addr;
677 + void *addr;
678 +
679 + if (ai->ai_family == AF_INET) {
680 + struct sockaddr_in *sa = (void *)ai->ai_addr;
681 + addr = &sa->sin_addr;
682 + } else if (ai->ai_family == AF_INET6) {
683 + struct sockaddr_in6 *sa6 = (void *)ai->ai_addr;
684 + addr = &sa6->sin6_addr;
685 + } else {
686 + die("unexpected address family: %d",
687 + ai->ai_family);
688 + }
689
679 - inet_ntop(AF_INET, &sin_addr->sin_addr,
690 + inet_ntop(ai->ai_family, addr,
691 addrbuf, sizeof(addrbuf));
692 strbuf_addstr(&hi->ip_address, addrbuf);
693