summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2005-09-23 05:12:50 +0000
committerPaul Eggert <eggert@cs.ucla.edu>2005-09-23 05:12:50 +0000
commit9fd31b8bebc2644e1586c10b74d0db9a10a8a52b (patch)
tree3750404d2843473b830df3296135708eff11c4a0 /lib
parentfc6f1f7f9c9872e19f9f1351c3f3afe77d7ad6bc (diff)
downloadcoreutils-9fd31b8bebc2644e1586c10b74d0db9a10a8a52b.tar.xz
* lib/getaddrinfo.c [HAVE_NETINET_IN_H]: Include <netinet/in.h>.
Problem reported by Eric Blake. (getaddrinfo): Initialize se so that it's not garbage. Redo internal storage allocation so that it doesn't make unportable assumptions about alignment. Fix a memory leak.
Diffstat (limited to 'lib')
-rw-r--r--lib/ChangeLog9
-rw-r--r--lib/getaddrinfo.c59
2 files changed, 52 insertions, 16 deletions
diff --git a/lib/ChangeLog b/lib/ChangeLog
index 0c0467395..167089063 100644
--- a/lib/ChangeLog
+++ b/lib/ChangeLog
@@ -1,3 +1,12 @@
+2005-09-22 Paul Eggert <eggert@cs.ucla.edu>
+
+ * getaddrinfo.c [HAVE_NETINET_IN_H]: Include <netinet/in.h>.
+ Problem reported by Eric Blake.
+ (getaddrinfo): Initialize se so that it's not garbage.
+ Redo internal storage allocation so that it doesn't make unportable
+ assumptions about alignment.
+ Fix a memory leak.
+
2005-09-21 Paul Eggert <eggert@cs.ucla.edu>
Sync from gnulib.
diff --git a/lib/getaddrinfo.c b/lib/getaddrinfo.c
index 594b76494..7ea696778 100644
--- a/lib/getaddrinfo.c
+++ b/lib/getaddrinfo.c
@@ -22,6 +22,10 @@
#include "getaddrinfo.h"
+#if HAVE_NETINET_IN_H
+# include <netinet/in.h>
+#endif
+
/* Get calloc. */
#include <stdlib.h>
@@ -62,9 +66,22 @@ getaddrinfo (const char *restrict nodename,
struct addrinfo **restrict res)
{
struct addrinfo *tmp;
- struct servent *se;
+ struct servent *se = NULL;
struct hostent *he;
- size_t sinlen;
+ void *storage;
+ size_t size;
+#if HAVE_IPV6
+ struct v6_pair {
+ struct addrinfo addrinfo;
+ struct sockaddr_in6 sockaddr_in6;
+ };
+#endif
+#if HAVE_IPV4
+ struct v4_pair {
+ struct addrinfo addrinfo;
+ struct sockaddr_in sockaddr_in;
+ };
+#endif
if (hints && (hints->ai_flags & ~AI_CANONNAME))
/* FIXME: Support more flags. */
@@ -103,13 +120,13 @@ getaddrinfo (const char *restrict nodename,
{
#if HAVE_IPV6
case PF_INET6:
- sinlen = sizeof (struct sockaddr_in6);
+ size = sizeof (struct v6_pair);
break;
#endif
#if HAVE_IPV4
case PF_INET:
- sinlen = sizeof (struct sockaddr_in);
+ size = sizeof (struct v4_pair);
break;
#endif
@@ -117,8 +134,8 @@ getaddrinfo (const char *restrict nodename,
return EAI_NODATA;
}
- tmp = calloc (1, sizeof (*tmp) + sinlen);
- if (!tmp)
+ storage = calloc (1, size);
+ if (!storage)
return EAI_MEMORY;
switch (he->h_addrtype)
@@ -126,18 +143,23 @@ getaddrinfo (const char *restrict nodename,
#if HAVE_IPV6
case PF_INET6:
{
- struct sockaddr_in6 *sinp = (char *) tmp + sizeof (*tmp);
+ struct v6_pair *p = storage;
+ struct sockaddr_in6 *sinp = &p->sockaddr_in6;
+ tmp = &p->addrinfo;
if (se)
sinp->sin6_port = se->s_port;
if (he->h_length != sizeof (sinp->sin6_addr))
- return EAI_SYSTEM; /* FIXME: Better return code? Set errno? */
+ {
+ free (storage);
+ return EAI_SYSTEM; /* FIXME: Better return code? Set errno? */
+ }
- memcpy (&sinp->sin6_addr, he->h_addr_list[0], he->h_length);
+ memcpy (&sinp->sin6_addr, he->h_addr_list[0], sizeof sinp->sin6_addr);
tmp->ai_addr = (struct sockaddr *) sinp;
- tmp->ai_addrlen = sinlen;
+ tmp->ai_addrlen = sizeof *sinp;
}
break;
#endif
@@ -145,24 +167,29 @@ getaddrinfo (const char *restrict nodename,
#if HAVE_IPV4
case PF_INET:
{
- struct sockaddr_in *sinp = (char *) tmp + sizeof (*tmp);
+ struct v4_pair *p = storage;
+ struct sockaddr_in *sinp = &p->sockaddr_in;
+ tmp = &p->addrinfo;
if (se)
sinp->sin_port = se->s_port;
if (he->h_length != sizeof (sinp->sin_addr))
- return EAI_SYSTEM; /* FIXME: Better return code? Set errno? */
+ {
+ free (storage);
+ return EAI_SYSTEM; /* FIXME: Better return code? Set errno? */
+ }
- memcpy (&sinp->sin_addr, he->h_addr_list[0], he->h_length);
+ memcpy (&sinp->sin_addr, he->h_addr_list[0], sizeof sinp->sin_addr);
tmp->ai_addr = (struct sockaddr *) sinp;
- tmp->ai_addrlen = sinlen;
+ tmp->ai_addrlen = sizeof *sinp;
}
break;
#endif
default:
- free (tmp);
+ free (storage);
return EAI_NODATA;
}
@@ -177,7 +204,7 @@ getaddrinfo (const char *restrict nodename,
tmp->ai_canonname = strdup (cn);
if (!tmp->ai_canonname)
{
- free (tmp);
+ free (storage);
return EAI_MEMORY;
}
}