Mercurial > hg > octave-nkf > gnulib-hg
changeset 6284:9500c37faeb5
* 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.
* m4/getaddrinfo.m4 (gl_PREREQ_GETADDRINFO): Undo previous change;
not needed, since socklen is a prerequisite module.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Fri, 23 Sep 2005 19:41:05 +0000 |
parents | b377d8796118 |
children | 6b58c8bc57fd |
files | lib/getaddrinfo.c m4/getaddrinfo.m4 |
diffstat | 2 files changed, 44 insertions(+), 18 deletions(-) [+] |
line wrap: on
line diff
--- 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 @@ 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 @@ { #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 @@ 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 @@ #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 @@ #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 @@ tmp->ai_canonname = strdup (cn); if (!tmp->ai_canonname) { - free (tmp); + free (storage); return EAI_MEMORY; } }
--- a/m4/getaddrinfo.m4 +++ b/m4/getaddrinfo.m4 @@ -1,4 +1,4 @@ -# getaddrinfo.m4 serial 5 +# getaddrinfo.m4 serial 4 dnl Copyright (C) 2004, 2005 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -13,7 +13,6 @@ # Prerequisites of lib/getaddrinfo.h and lib/getaddrinfo.c. AC_DEFUN([gl_PREREQ_GETADDRINFO], [ - AC_REQUIRE([gl_SOCKLEN_T]) AC_REQUIRE([gl_C_RESTRICT]) AC_REQUIRE([gl_SOCKET_FAMILIES]) AC_REQUIRE([AC_C_INLINE])