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 <benny.kra@googlemail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Benjamin Kramer committed
Apr 27, 2009 at 15:59 UTC
3e8a00ae1d529e61f38f36fdb504902064cf1f5d
1 file changed
+14
-14
daemon.c
+14
-14
@@ -444,27 +444,27 @@ static void parse_extra_args(char *extra_args, int buflen)
444
if (hostname) {
445
#ifndef NO_IPV6
446
struct addrinfo hints;
447
- struct addrinfo *ai, *ai0;
447
+ struct addrinfo *ai;
448
int gai;
449
static char addrbuf[HOST_NAME_MAX + 1];
450
451
memset(&hints, 0, sizeof(hints));
452
hints.ai_flags = AI_CANONNAME;
453
454
- gai = getaddrinfo(hostname, 0, &hints, &ai0);
454
+ gai = getaddrinfo(hostname, 0, &hints, &ai);
455
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);
468
}
469
#else
470
struct hostent *hent;