From 79248fe718689c31c3df3d24bfbc6f147bc345e7 Mon Sep 17 00:00:00 2001 From: Manuel Novoa III Date: Wed, 10 Mar 2004 20:43:23 +0000 Subject: Bug fix: gethostbyname2_r would fail if /etc/host was missing. Bug fix: gethostbyname_r checked errno without first setting it to a known value. --- libc/inet/resolv.c | 59 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 21 deletions(-) (limited to 'libc/inet') diff --git a/libc/inet/resolv.c b/libc/inet/resolv.c index 592582133..0c9b52415 100644 --- a/libc/inet/resolv.c +++ b/libc/inet/resolv.c @@ -1860,20 +1860,26 @@ int gethostbyname_r(const char * name, return EINVAL; /* do /etc/hosts first */ - if ((i=__get_hosts_byname_r(name, AF_INET, result_buf, - buf, buflen, result, h_errnop))==0) - return i; - switch (*h_errnop) { - case HOST_NOT_FOUND: - case NO_ADDRESS: - break; - case NETDB_INTERNAL: - if (errno == ENOENT) { - break; - } - /* else fall through */ - default: + { + int old_errno = errno; /* Save the old errno and reset errno */ + __set_errno(0); /* to check for missing /etc/hosts. */ + + if ((i=__get_hosts_byname_r(name, AF_INET, result_buf, + buf, buflen, result, h_errnop))==0) return i; + switch (*h_errnop) { + case HOST_NOT_FOUND: + case NO_ADDRESS: + break; + case NETDB_INTERNAL: + if (errno == ENOENT) { + break; + } + /* else fall through */ + default: + return i; + } + __set_errno(old_errno); } DPRINTF("Nothing found in /etc/hosts\n"); @@ -1996,15 +2002,26 @@ int gethostbyname2_r(const char *name, int family, return EINVAL; /* do /etc/hosts first */ - if ((i=__get_hosts_byname_r(name, family, result_buf, - buf, buflen, result, h_errnop))==0) - return i; - switch (*h_errnop) { - case HOST_NOT_FOUND: - case NO_ADDRESS: - break; - default: + { + int old_errno = errno; /* Save the old errno and reset errno */ + __set_errno(0); /* to check for missing /etc/hosts. */ + + if ((i=__get_hosts_byname_r(name, AF_INET, result_buf, + buf, buflen, result, h_errnop))==0) return i; + switch (*h_errnop) { + case HOST_NOT_FOUND: + case NO_ADDRESS: + break; + case NETDB_INTERNAL: + if (errno == ENOENT) { + break; + } + /* else fall through */ + default: + return i; + } + __set_errno(old_errno); } DPRINTF("Nothing found in /etc/hosts\n"); -- cgit v1.2.3