daemon: fix IPv6 address truncation in ip2str()

The sockaddr struct size (ai_addrlen) is passed as the output buffer size to inet_ntop(). For IPv6, sizeof(sockaddr_in6) is 28 bytes but INET6_ADDRSTRLEN is 46, so long IPv6 addresses are silently truncated. Fix this by passing sizeof(ip) instead, which is the actual size of the destination buffer. Drop the now-unused len parameter from ip2str() and update all callers. 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 30c8fda1ab6d55d3b0129bb1686c23bf06cd5b0d
1 file changed +7 -7
daemon.c
+7 -7
@@ -947,7 +947,7 @@ struct socketlist {
947 size_t alloc;
948 };
949
950 -static const char *ip2str(int family, struct sockaddr *sin, socklen_t len)
950 +static const char *ip2str(int family, struct sockaddr *sin)
951 {
952 #ifdef NO_IPV6
953 static char ip[INET_ADDRSTRLEN];
@@ -958,11 +958,11 @@ static const char *ip2str(int family, struct sockaddr *sin, socklen_t len)
958 switch (family) {
959 #ifndef NO_IPV6
960 case AF_INET6:
961 - inet_ntop(family, &((struct sockaddr_in6*)sin)->sin6_addr, ip, len);
961 + inet_ntop(family, &((struct sockaddr_in6*)sin)->sin6_addr, ip, sizeof(ip));
962 break;
963 #endif
964 case AF_INET:
965 - inet_ntop(family, &((struct sockaddr_in*)sin)->sin_addr, ip, len);
965 + inet_ntop(family, &((struct sockaddr_in*)sin)->sin_addr, ip, sizeof(ip));
966 break;
967 default:
968 xsnprintf(ip, sizeof(ip), "<unknown>");
@@ -1019,14 +1019,14 @@ static int setup_named_sock(char *listen_addr, int listen_port, struct socketlis
1019
1020 if (bind(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
1021 logerror("Could not bind to %s: %s",
1022 - ip2str(ai->ai_family, ai->ai_addr, ai->ai_addrlen),
1022 + ip2str(ai->ai_family, ai->ai_addr),
1023 strerror(errno));
1024 close(sockfd);
1025 continue; /* not fatal */
1026 }
1027 if (listen(sockfd, 5) < 0) {
1028 logerror("Could not listen to %s: %s",
1029 - ip2str(ai->ai_family, ai->ai_addr, ai->ai_addrlen),
1029 + ip2str(ai->ai_family, ai->ai_addr),
1030 strerror(errno));
1031 close(sockfd);
1032 continue; /* not fatal */
@@ -1080,7 +1080,7 @@ static int setup_named_sock(char *listen_addr, int listen_port, struct socketlis
1080
1081 if ( bind(sockfd, (struct sockaddr *)&sin, sizeof sin) < 0 ) {
1082 logerror("Could not bind to %s: %s",
1083 - ip2str(AF_INET, (struct sockaddr *)&sin, sizeof(sin)),
1083 + ip2str(AF_INET, (struct sockaddr *)&sin),
1084 strerror(errno));
1085 close(sockfd);
1086 return 0;
@@ -1088,7 +1088,7 @@ static int setup_named_sock(char *listen_addr, int listen_port, struct socketlis
1088
1089 if (listen(sockfd, 5) < 0) {
1090 logerror("Could not listen to %s: %s",
1091 - ip2str(AF_INET, (struct sockaddr *)&sin, sizeof(sin)),
1091 + ip2str(AF_INET, (struct sockaddr *)&sin),
1092 strerror(errno));
1093 close(sockfd);
1094 return 0;