git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 1/3] daemon: fix IPv6 address corruption in lookup_hostname()

From
Junio C Hamano <gitster@pobox.com>
Date
May 14, 2026, 21:26 UTC
Message-ID
<xmqqmry1el8b.fsf@gitster.g>
In-Reply-To
<b2d81438117a716417a031c74b678a8f91701af4.1778773592.git.gitgitgadget@gmail.com>
"Sebastien Tardif via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 38 quoted lines
> From: Sebastien Tardif <sebtardif@ncf.ca>
>
> 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>
> ---
>  daemon.c | 15 +++++++++++++--
>  1 file changed, 13 insertions(+), 2 deletions(-)
>
> diff --git a/daemon.c b/daemon.c
> index 0a7b1aae44..80fa0226d8 100644
> --- a/daemon.c
> +++ b/daemon.c
> @@ -674,9 +674,20 @@ static void lookup_hostname(struct hostinfo *hi)
>  
>  		gai = getaddrinfo(hi->hostname.buf, NULL, &hints, &ai);
>  		if (!gai) {
> -			struct sockaddr_in *sin_addr = (void *)ai->ai_addr;
> +			void *addr;
> +
> +			if (ai->ai_family == AF_INET) {
> +				struct sockaddr_in *sa = (void *)ai->ai_addr;
> +				addr = &sa->sin_addr;
> +			} else if (ai->ai_family == AF_INET6) {
> +				struct sockaddr_in6 *sa6 = (void *)ai->ai_addr;
> +				addr = &sa6->sin6_addr;
> +			} else {
> +				die("unexpected address family: %d",
> +				    ai->ai_family);
> +			}

The previous iteration used to more explicitly cast ai->ai_addr to the target type, but the use of (void *) here is a cute way to make the result shorter, which makes it a bit easier to read (it may take readers a bit of practice to convince themselves that this type conversion using (void *) as an intermediate type is perfectly fine, though).

Show 5 quoted lines
>  
> -			inet_ntop(AF_INET, &sin_addr->sin_addr,
> +			inet_ntop(ai->ai_family, addr,
>  				  addrbuf, sizeof(addrbuf));
>  			strbuf_addstr(&hi->ip_address, addrbuf);
Previous: Sebastien Tardif via GitGitGadgetNext: Sebastien Tardif via GitGitGadget
Message 3 of 16 in “daemon: fix network address handling bugs”
  1. 0/3 daemon: fix network address handling bugsSebastien Tardif via GitGitGadget, May 14, 2026
  2. 1/3 daemon: fix IPv6 address corruption in lookup_hostname()Sebastien Tardif via GitGitGadget, May 14, 2026
  3. Junio C HamanoMay 14, 2026
  4. 2/3 daemon: fix IPv6 address truncation in ip2str()Sebastien Tardif via GitGitGadget, May 14, 2026
  5. 3/3 daemon: guard NULL REMOTE_PORT in execute() loggingSebastien Tardif via GitGitGadget, May 14, 2026
  6. Junio C HamanoMay 14, 2026
  7. Patrick SteinhardtMay 15, 2026
  8. 0/3 daemon: fix network address handling bugsSebastien Tardif via GitGitGadget, May 27, 2026
  9. 1/3 daemon: fix IPv6 address corruption in lookup_hostname()Sebastien Tardif via GitGitGadget, May 27, 2026
  10. 2/3 daemon: fix IPv6 address truncation in ip2str()Sebastien Tardif via GitGitGadget, May 27, 2026
  11. 3/3 daemon: guard NULL REMOTE_PORT in execute() loggingSebastien Tardif via GitGitGadget, May 27, 2026
  12. Junio C HamanoMay 27, 2026
  13. 0/3 daemon: fix network address handling bugsSebastien Tardif via GitGitGadget, May 28, 2026
  14. 1/3 daemon: fix IPv6 address corruption in lookup_hostname()Sebastien Tardif via GitGitGadget, May 28, 2026
  15. 2/3 daemon: fix IPv6 address truncation in ip2str()Sebastien Tardif via GitGitGadget, May 28, 2026
  16. 3/3 daemon: guard NULL REMOTE_PORT in execute() loggingSebastien Tardif via GitGitGadget, May 28, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.