diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2003-06-09 17:59:19 +0000 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2003-06-09 17:59:19 +0000 |
commit | cdfb3d99811d4b1c99c2e0c29ff8cba5043f0c91 (patch) | |
tree | 87b92fbdf9854bd77bc420a4fae1cc6eb25a4e9d /src/backend/libpq/ip.c | |
parent | 2df532d9a2b79f27f17235b3b125bd0ab977b7d8 (diff) |
freeaddrinfo2() does need two parameters after all, per comment by
Kurt Roeckx. Add some documentation to try to prevent others from
repeating my mistake.
Diffstat (limited to 'src/backend/libpq/ip.c')
-rw-r--r-- | src/backend/libpq/ip.c | 49 |
1 files changed, 31 insertions, 18 deletions
diff --git a/src/backend/libpq/ip.c b/src/backend/libpq/ip.c index 83a5145c363..948fb576141 100644 --- a/src/backend/libpq/ip.c +++ b/src/backend/libpq/ip.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/libpq/ip.c,v 1.8 2003/06/08 17:42:59 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/libpq/ip.c,v 1.9 2003/06/09 17:59:19 tgl Exp $ * * This file and the IPV6 implementation were initially provided by * Nigel Kukard <nkukard@lbsd.net>, Linux Based Systems Design @@ -73,26 +73,34 @@ getaddrinfo2(const char *hostname, const char *servname, /* * freeaddrinfo2 - free addrinfo structures for IPv4, IPv6, or Unix + * + * Note: the ai_family field of the original hint structure must be passed + * so that we can tell whether the addrinfo struct was built by the system's + * getaddrinfo() routine or our own getaddrinfo_unix() routine. Some versions + * of getaddrinfo() might be willing to return AF_UNIX addresses, so it's + * not safe to look at ai_family in the addrinfo itself. */ void -freeaddrinfo2(struct addrinfo *ai) +freeaddrinfo2(int hint_ai_family, struct addrinfo *ai) { - if (ai != NULL) - { #ifdef HAVE_UNIX_SOCKETS - if (ai->ai_family == AF_UNIX) + if (hint_ai_family == AF_UNIX) + { + /* struct was built by getaddrinfo_unix (see getaddrinfo2) */ + while (ai != NULL) { - while (ai != NULL) - { - struct addrinfo *p = ai; - - ai = ai->ai_next; - free(p->ai_addr); - free(p); - } + struct addrinfo *p = ai; + + ai = ai->ai_next; + free(p->ai_addr); + free(p); } - else + } + else #endif /* HAVE_UNIX_SOCKETS */ + { + /* struct was built by getaddrinfo() */ + if (ai != NULL) freeaddrinfo(ai); } } @@ -115,6 +123,8 @@ getaddrinfo_unix(const char *path, const struct addrinfo *hintsp, struct addrinfo *aip; struct sockaddr_un *unp; + *result = NULL; + MemSet(&hints, 0, sizeof(hints)); if (hintsp == NULL) @@ -138,6 +148,13 @@ getaddrinfo_unix(const char *path, const struct addrinfo *hintsp, if (aip == NULL) return EAI_MEMORY; + unp = calloc(1, sizeof(struct sockaddr_un)); + if (unp == NULL) + { + free(aip); + return EAI_MEMORY; + } + aip->ai_family = AF_UNIX; aip->ai_socktype = hints.ai_socktype; aip->ai_protocol = hints.ai_protocol; @@ -145,10 +162,6 @@ getaddrinfo_unix(const char *path, const struct addrinfo *hintsp, aip->ai_canonname = NULL; *result = aip; - unp = calloc(1, sizeof(struct sockaddr_un)); - if (aip == NULL) - return EAI_MEMORY; - unp->sun_family = AF_UNIX; aip->ai_addr = (struct sockaddr *) unp; aip->ai_addrlen = sizeof(struct sockaddr_un); |