daemon: handle gethostbyname() error

If the user-supplied hostname can't be found then we should not use it. We already avoid doing that in the non-NO_IPV6 case by checking if the return value of getaddrinfo() is zero (success). Do the same in the NO_IPV6 case and make sure the return value of gethostbyname() isn't NULL before dereferencing this pointer. Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Oct 1, 2014 at 12:16 UTC eb6c403500dd0b0d78b7b00d7ed0bf6b5daccc4e
1 file changed +14 -13
daemon.c
+14 -13
@@ -579,20 +579,21 @@ static void parse_host_arg(char *extra_args, int buflen)
579 static char addrbuf[HOST_NAME_MAX + 1];
580
581 hent = gethostbyname(hostname);
582 + if (hent) {
583 + ap = hent->h_addr_list;
584 + memset(&sa, 0, sizeof sa);
585 + sa.sin_family = hent->h_addrtype;
586 + sa.sin_port = htons(0);
587 + memcpy(&sa.sin_addr, *ap, hent->h_length);
588 +
589 + inet_ntop(hent->h_addrtype, &sa.sin_addr,
590 + addrbuf, sizeof(addrbuf));
591
583 - ap = hent->h_addr_list;
584 - memset(&sa, 0, sizeof sa);
585 - sa.sin_family = hent->h_addrtype;
586 - sa.sin_port = htons(0);
587 - memcpy(&sa.sin_addr, *ap, hent->h_length);
588 -
589 - inet_ntop(hent->h_addrtype, &sa.sin_addr,
590 - addrbuf, sizeof(addrbuf));
591 -
592 - free(canon_hostname);
593 - canon_hostname = xstrdup(hent->h_name);
594 - free(ip_address);
595 - ip_address = xstrdup(addrbuf);
592 + free(canon_hostname);
593 + canon_hostname = xstrdup(hent->h_name);
594 + free(ip_address);
595 + ip_address = xstrdup(addrbuf);
596 + }
597 #endif
598 }
599 }