getnameinfo: Use struct scratch_buffer instead of extend_alloca

This patch adjusts the internal function nrl_domainname, too.
This commit is contained in:
Florian Weimer 2015-04-08 20:56:35 +02:00
parent 794a74af4d
commit c6ee40da8b
2 changed files with 59 additions and 27 deletions

View File

@ -11,6 +11,7 @@
grp/compat-initgroups.c. grp/compat-initgroups.c.
* nis/nss_compat/compat-initgroups.c (_nss_compat_initgroups_dyn): * nis/nss_compat/compat-initgroups.c (_nss_compat_initgroups_dyn):
Rewrite to use struct scratch_buffer instead of extend_alloca. Rewrite to use struct scratch_buffer instead of extend_alloca.
* inet/getnameinfo.c (nrl_domainname, getnameinfo): Likewise.
2015-04-08 Joseph Myers <joseph@codesourcery.com> 2015-04-08 Joseph Myers <joseph@codesourcery.com>

View File

@ -35,7 +35,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
/* This software is Copyright 1996 by Craig Metz, All Rights Reserved. */ /* This software is Copyright 1996 by Craig Metz, All Rights Reserved. */
#include <alloca.h>
#include <errno.h> #include <errno.h>
#include <netdb.h> #include <netdb.h>
#include <stddef.h> #include <stddef.h>
@ -53,6 +52,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <sys/un.h> #include <sys/un.h>
#include <sys/utsname.h> #include <sys/utsname.h>
#include <bits/libc-lock.h> #include <bits/libc-lock.h>
#include <scratch_buffer.h>
#ifdef HAVE_LIBIDN #ifdef HAVE_LIBIDN
# include <libidn/idna.h> # include <libidn/idna.h>
@ -82,17 +82,21 @@ nrl_domainname (void)
{ {
char *c; char *c;
struct hostent *h, th; struct hostent *h, th;
size_t tmpbuflen = 1024;
char *tmpbuf = alloca (tmpbuflen);
int herror; int herror;
struct scratch_buffer tmpbuf;
scratch_buffer_init (&tmpbuf);
not_first = 1; not_first = 1;
while (__gethostbyname_r ("localhost", &th, tmpbuf, tmpbuflen, &h, while (__gethostbyname_r ("localhost", &th,
&herror)) tmpbuf.data, tmpbuf.length,
&h, &herror))
{ {
if (herror == NETDB_INTERNAL && errno == ERANGE) if (herror == NETDB_INTERNAL && errno == ERANGE)
tmpbuf = extend_alloca (tmpbuf, tmpbuflen, 2 * tmpbuflen); {
if (!scratch_buffer_grow (&tmpbuf))
goto done;
}
else else
break; break;
} }
@ -103,22 +107,26 @@ nrl_domainname (void)
{ {
/* The name contains no domain information. Use the name /* The name contains no domain information. Use the name
now to get more information. */ now to get more information. */
while (__gethostname (tmpbuf, tmpbuflen)) while (__gethostname (tmpbuf.data, tmpbuf.length))
tmpbuf = extend_alloca (tmpbuf, tmpbuflen, 2 * tmpbuflen); if (!scratch_buffer_grow (&tmpbuf))
goto done;
if ((c = strchr (tmpbuf, '.'))) if ((c = strchr (tmpbuf.data, '.')))
domain = __strdup (++c); domain = __strdup (++c);
else else
{ {
/* We need to preserve the hostname. */ /* We need to preserve the hostname. */
const char *hstname = strdupa (tmpbuf); const char *hstname = strdupa (tmpbuf.data);
while (__gethostbyname_r (hstname, &th, tmpbuf, tmpbuflen, while (__gethostbyname_r (hstname, &th,
tmpbuf.data, tmpbuf.length,
&h, &herror)) &h, &herror))
{ {
if (herror == NETDB_INTERNAL && errno == ERANGE) if (herror == NETDB_INTERNAL && errno == ERANGE)
tmpbuf = extend_alloca (tmpbuf, tmpbuflen, {
2 * tmpbuflen); if (!scratch_buffer_grow (&tmpbuf))
goto done;
}
else else
break; break;
} }
@ -133,12 +141,15 @@ nrl_domainname (void)
while (__gethostbyaddr_r ((const char *) &in_addr, while (__gethostbyaddr_r ((const char *) &in_addr,
sizeof (struct in_addr), sizeof (struct in_addr),
AF_INET, &th, tmpbuf, AF_INET, &th,
tmpbuflen, &h, &herror)) tmpbuf.data, tmpbuf.length,
&h, &herror))
{ {
if (herror == NETDB_INTERNAL && errno == ERANGE) if (herror == NETDB_INTERNAL && errno == ERANGE)
tmpbuf = extend_alloca (tmpbuf, tmpbuflen, {
2 * tmpbuflen); if (!scratch_buffer_grow (&tmpbuf))
goto done;
}
else else
break; break;
} }
@ -148,6 +159,8 @@ nrl_domainname (void)
} }
} }
} }
done:
scratch_buffer_free (&tmpbuf);
} }
__libc_lock_unlock (lock); __libc_lock_unlock (lock);
@ -163,11 +176,12 @@ getnameinfo (const struct sockaddr *sa, socklen_t addrlen, char *host,
int flags) int flags)
{ {
int serrno = errno; int serrno = errno;
int tmpbuflen = 1024;
int herrno; int herrno;
char *tmpbuf = alloca (tmpbuflen);
struct hostent th; struct hostent th;
int ok = 0; int ok = 0;
struct scratch_buffer tmpbuf;
scratch_buffer_init (&tmpbuf);
if (flags & ~(NI_NUMERICHOST|NI_NUMERICSERV|NI_NOFQDN|NI_NAMEREQD|NI_DGRAM if (flags & ~(NI_NUMERICHOST|NI_NUMERICSERV|NI_NOFQDN|NI_NAMEREQD|NI_DGRAM
#ifdef HAVE_LIBIDN #ifdef HAVE_LIBIDN
@ -212,21 +226,35 @@ getnameinfo (const struct sockaddr *sa, socklen_t addrlen, char *host,
{ {
while (__gethostbyaddr_r ((const void *) &(((const struct sockaddr_in6 *) sa)->sin6_addr), while (__gethostbyaddr_r ((const void *) &(((const struct sockaddr_in6 *) sa)->sin6_addr),
sizeof(struct in6_addr), sizeof(struct in6_addr),
AF_INET6, &th, tmpbuf, tmpbuflen, AF_INET6, &th,
tmpbuf.data, tmpbuf.length,
&h, &herrno)) &h, &herrno))
if (herrno == NETDB_INTERNAL && errno == ERANGE) if (herrno == NETDB_INTERNAL && errno == ERANGE)
tmpbuf = extend_alloca (tmpbuf, tmpbuflen, 2 * tmpbuflen); {
if (!scratch_buffer_grow (&tmpbuf))
{
__set_h_errno (herrno);
return EAI_MEMORY;
}
}
else else
break; break;
} }
else else
{ {
while (__gethostbyaddr_r ((const void *) &(((const struct sockaddr_in *)sa)->sin_addr), while (__gethostbyaddr_r ((const void *) &(((const struct sockaddr_in *)sa)->sin_addr),
sizeof(struct in_addr), AF_INET, sizeof(struct in_addr),
&th, tmpbuf, tmpbuflen, AF_INET, &th,
tmpbuf.data, tmpbuf.length,
&h, &herrno)) &h, &herrno))
if (herrno == NETDB_INTERNAL && errno == ERANGE) if (herrno == NETDB_INTERNAL && errno == ERANGE)
tmpbuf = extend_alloca (tmpbuf, tmpbuflen, 2 * tmpbuflen); {
if (!scratch_buffer_grow (&tmpbuf))
{
__set_h_errno (herrno);
return EAI_MEMORY;
}
}
else else
break; break;
} }
@ -401,11 +429,14 @@ getnameinfo (const struct sockaddr *sa, socklen_t addrlen, char *host,
int e; int e;
while ((e = __getservbyport_r (((const struct sockaddr_in *) sa)->sin_port, while ((e = __getservbyport_r (((const struct sockaddr_in *) sa)->sin_port,
((flags & NI_DGRAM) ((flags & NI_DGRAM)
? "udp" : "tcp"), ? "udp" : "tcp"), &ts,
&ts, tmpbuf, tmpbuflen, &s))) tmpbuf.data, tmpbuf.length, &s)))
{ {
if (e == ERANGE) if (e == ERANGE)
tmpbuf = extend_alloca (tmpbuf, tmpbuflen, 2 * tmpbuflen); {
if (!scratch_buffer_grow (&tmpbuf))
return EAI_MEMORY;
}
else else
break; break;
} }