Skip to content

Commit eb6c403

Browse files
rscharfegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e6aaa39 commit eb6c403

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

daemon.c

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -579,20 +579,21 @@ static void parse_host_arg(char *extra_args, int buflen)
579579
static char addrbuf[HOST_NAME_MAX + 1];
580580

581581
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));
582591

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+
}
596597
#endif
597598
}
598599
}

0 commit comments

Comments
 (0)