Skip to content

Commit 3e8a00a

Browse files
d0kgitster
authored andcommitted
daemon.c: fix segfault on OS X
On OS X (and maybe other unices), getaddrinfo(3) returns NULL in the ai_canonname field if it's called with an IP address for the hostname. We'll now use the IP address for the hostname if ai_canonname was NULL, this also matches the behaviour on Linux. steps to reproduce: $ git daemon --export-all $ git clone git://127.0.0.1/frotz => git daemon's fork (silently) segfaults. Remove the pointless loop while at it. There is only one iteration because of the break; on the last line and there are no continues. Signed-off-by: Benjamin Kramer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0c44c94 commit 3e8a00a

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

daemon.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -444,27 +444,27 @@ static void parse_extra_args(char *extra_args, int buflen)
444444
if (hostname) {
445445
#ifndef NO_IPV6
446446
struct addrinfo hints;
447-
struct addrinfo *ai, *ai0;
447+
struct addrinfo *ai;
448448
int gai;
449449
static char addrbuf[HOST_NAME_MAX + 1];
450450

451451
memset(&hints, 0, sizeof(hints));
452452
hints.ai_flags = AI_CANONNAME;
453453

454-
gai = getaddrinfo(hostname, 0, &hints, &ai0);
454+
gai = getaddrinfo(hostname, 0, &hints, &ai);
455455
if (!gai) {
456-
for (ai = ai0; ai; ai = ai->ai_next) {
457-
struct sockaddr_in *sin_addr = (void *)ai->ai_addr;
458-
459-
inet_ntop(AF_INET, &sin_addr->sin_addr,
460-
addrbuf, sizeof(addrbuf));
461-
free(canon_hostname);
462-
canon_hostname = xstrdup(ai->ai_canonname);
463-
free(ip_address);
464-
ip_address = xstrdup(addrbuf);
465-
break;
466-
}
467-
freeaddrinfo(ai0);
456+
struct sockaddr_in *sin_addr = (void *)ai->ai_addr;
457+
458+
inet_ntop(AF_INET, &sin_addr->sin_addr,
459+
addrbuf, sizeof(addrbuf));
460+
free(ip_address);
461+
ip_address = xstrdup(addrbuf);
462+
463+
free(canon_hostname);
464+
canon_hostname = xstrdup(ai->ai_canonname ?
465+
ai->ai_canonname : ip_address);
466+
467+
freeaddrinfo(ai);
468468
}
469469
#else
470470
struct hostent *hent;

0 commit comments

Comments
 (0)