Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit cdfb3d9

Browse files
committed
freeaddrinfo2() does need two parameters after all, per comment by
Kurt Roeckx. Add some documentation to try to prevent others from repeating my mistake.
1 parent 2df532d commit cdfb3d9

File tree

5 files changed

+48
-34
lines changed

5 files changed

+48
-34
lines changed

src/backend/libpq/ip.c

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/libpq/ip.c,v 1.8 2003/06/08 17:42:59 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/libpq/ip.c,v 1.9 2003/06/09 17:59:19 tgl Exp $
1212
*
1313
* This file and the IPV6 implementation were initially provided by
1414
* Nigel Kukard <nkukard@lbsd.net>, Linux Based Systems Design
@@ -73,26 +73,34 @@ getaddrinfo2(const char *hostname, const char *servname,
7373

7474
/*
7575
* freeaddrinfo2 - free addrinfo structures for IPv4, IPv6, or Unix
76+
*
77+
* Note: the ai_family field of the original hint structure must be passed
78+
* so that we can tell whether the addrinfo struct was built by the system's
79+
* getaddrinfo() routine or our own getaddrinfo_unix() routine. Some versions
80+
* of getaddrinfo() might be willing to return AF_UNIX addresses, so it's
81+
* not safe to look at ai_family in the addrinfo itself.
7682
*/
7783
void
78-
freeaddrinfo2(struct addrinfo *ai)
84+
freeaddrinfo2(int hint_ai_family, struct addrinfo *ai)
7985
{
80-
if (ai != NULL)
81-
{
8286
#ifdef HAVE_UNIX_SOCKETS
83-
if (ai->ai_family == AF_UNIX)
87+
if (hint_ai_family == AF_UNIX)
88+
{
89+
/* struct was built by getaddrinfo_unix (see getaddrinfo2) */
90+
while (ai != NULL)
8491
{
85-
while (ai != NULL)
86-
{
87-
struct addrinfo *p = ai;
88-
89-
ai = ai->ai_next;
90-
free(p->ai_addr);
91-
free(p);
92-
}
92+
struct addrinfo *p = ai;
93+
94+
ai = ai->ai_next;
95+
free(p->ai_addr);
96+
free(p);
9397
}
94-
else
98+
}
99+
else
95100
#endif /* HAVE_UNIX_SOCKETS */
101+
{
102+
/* struct was built by getaddrinfo() */
103+
if (ai != NULL)
96104
freeaddrinfo(ai);
97105
}
98106
}
@@ -115,6 +123,8 @@ getaddrinfo_unix(const char *path, const struct addrinfo *hintsp,
115123
struct addrinfo *aip;
116124
struct sockaddr_un *unp;
117125

126+
*result = NULL;
127+
118128
MemSet(&hints, 0, sizeof(hints));
119129

120130
if (hintsp == NULL)
@@ -138,17 +148,20 @@ getaddrinfo_unix(const char *path, const struct addrinfo *hintsp,
138148
if (aip == NULL)
139149
return EAI_MEMORY;
140150

151+
unp = calloc(1, sizeof(struct sockaddr_un));
152+
if (unp == NULL)
153+
{
154+
free(aip);
155+
return EAI_MEMORY;
156+
}
157+
141158
aip->ai_family = AF_UNIX;
142159
aip->ai_socktype = hints.ai_socktype;
143160
aip->ai_protocol = hints.ai_protocol;
144161
aip->ai_next = NULL;
145162
aip->ai_canonname = NULL;
146163
*result = aip;
147164

148-
unp = calloc(1, sizeof(struct sockaddr_un));
149-
if (aip == NULL)
150-
return EAI_MEMORY;
151-
152165
unp->sun_family = AF_UNIX;
153166
aip->ai_addr = (struct sockaddr *) unp;
154167
aip->ai_addrlen = sizeof(struct sockaddr_un);

src/backend/libpq/pqcomm.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
3131
* Portions Copyright (c) 1994, Regents of the University of California
3232
*
33-
* $Header: /cvsroot/pgsql/src/backend/libpq/pqcomm.c,v 1.155 2003/06/08 17:43:00 tgl Exp $
33+
* $Header: /cvsroot/pgsql/src/backend/libpq/pqcomm.c,v 1.156 2003/06/09 17:59:19 tgl Exp $
3434
*
3535
*-------------------------------------------------------------------------
3636
*/
@@ -242,15 +242,15 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
242242
{
243243
elog(LOG, "server socket failure: getaddrinfo2(): %s",
244244
gai_strerror(ret));
245-
freeaddrinfo2(addrs);
245+
freeaddrinfo2(hint.ai_family, addrs);
246246
return STATUS_ERROR;
247247
}
248248

249249
if ((fd = socket(family, SOCK_STREAM, 0)) < 0)
250250
{
251251
elog(LOG, "server socket failure: socket(): %s",
252252
strerror(errno));
253-
freeaddrinfo2(addrs);
253+
freeaddrinfo2(hint.ai_family, addrs);
254254
return STATUS_ERROR;
255255
}
256256

@@ -261,7 +261,7 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
261261
{
262262
elog(LOG, "server socket failure: setsockopt(SO_REUSEADDR): %s",
263263
strerror(errno));
264-
freeaddrinfo2(addrs);
264+
freeaddrinfo2(hint.ai_family, addrs);
265265
return STATUS_ERROR;
266266
}
267267
}
@@ -278,7 +278,7 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
278278
sock_path);
279279
else
280280
elog(LOG, "\tIf not, wait a few seconds and retry.");
281-
freeaddrinfo2(addrs);
281+
freeaddrinfo2(hint.ai_family, addrs);
282282
return STATUS_ERROR;
283283
}
284284

@@ -287,7 +287,7 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
287287
{
288288
if (Setup_AF_UNIX() != STATUS_OK)
289289
{
290-
freeaddrinfo2(addrs);
290+
freeaddrinfo2(hint.ai_family, addrs);
291291
return STATUS_ERROR;
292292
}
293293
}
@@ -307,14 +307,13 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
307307
{
308308
elog(LOG, "server socket failure: listen(): %s",
309309
strerror(errno));
310-
freeaddrinfo2(addrs);
310+
freeaddrinfo2(hint.ai_family, addrs);
311311
return STATUS_ERROR;
312312
}
313313

314314
*fdP = fd;
315-
freeaddrinfo2(addrs);
315+
freeaddrinfo2(hint.ai_family, addrs);
316316
return STATUS_OK;
317-
318317
}
319318

320319

src/include/libpq/ip.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* Copyright (c) 2003, PostgreSQL Global Development Group
77
*
8-
* $Id: ip.h,v 1.4 2003/06/08 17:43:00 tgl Exp $
8+
* $Id: ip.h,v 1.5 2003/06/09 17:59:19 tgl Exp $
99
*
1010
*-------------------------------------------------------------------------
1111
*/
@@ -19,7 +19,7 @@
1919
extern int getaddrinfo2(const char *hostname, const char *servname,
2020
const struct addrinfo *hintp,
2121
struct addrinfo **result);
22-
extern void freeaddrinfo2(struct addrinfo *ai);
22+
extern void freeaddrinfo2(int hint_ai_family, struct addrinfo *ai);
2323

2424
extern char *SockAddr_ntop(const SockAddr *sa, char *dst, size_t cnt,
2525
int v4conv);

src/interfaces/libpq/fe-connect.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.242 2003/06/08 17:43:00 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.243 2003/06/09 17:59:19 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -848,7 +848,7 @@ connectDBStart(PGconn *conn)
848848
printfPQExpBuffer(&conn->errorMessage,
849849
libpq_gettext("getaddrinfo() failed: %s\n"),
850850
gai_strerror(ret));
851-
freeaddrinfo2(addrs);
851+
freeaddrinfo2(hint.ai_family, addrs);
852852
goto connect_errReturn;
853853
}
854854

@@ -857,6 +857,7 @@ connectDBStart(PGconn *conn)
857857
*/
858858
conn->addrlist = addrs;
859859
conn->addr_cur = addrs;
860+
conn->addrlist_family = hint.ai_family;
860861
conn->pversion = PG_PROTOCOL(3,0);
861862
conn->status = CONNECTION_NEEDED;
862863

@@ -1686,7 +1687,7 @@ PQconnectPoll(PGconn *conn)
16861687
}
16871688

16881689
/* We can release the address list now. */
1689-
freeaddrinfo2(conn->addrlist);
1690+
freeaddrinfo2(conn->addrlist_family, conn->addrlist);
16901691
conn->addrlist = NULL;
16911692
conn->addr_cur = NULL;
16921693

@@ -1858,7 +1859,7 @@ freePGconn(PGconn *conn)
18581859
/* Note that conn->Pfdebug is not ours to close or free */
18591860
if (conn->notifyList)
18601861
DLFreeList(conn->notifyList);
1861-
freeaddrinfo2(conn->addrlist);
1862+
freeaddrinfo2(conn->addrlist_family, conn->addrlist);
18621863
if (conn->lobjfuncs)
18631864
free(conn->lobjfuncs);
18641865
if (conn->inBuffer)

src/interfaces/libpq/libpq-int.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
1313
* Portions Copyright (c) 1994, Regents of the University of California
1414
*
15-
* $Id: libpq-int.h,v 1.71 2003/06/08 17:43:00 tgl Exp $
15+
* $Id: libpq-int.h,v 1.72 2003/06/09 17:59:19 tgl Exp $
1616
*
1717
*-------------------------------------------------------------------------
1818
*/
@@ -270,6 +270,7 @@ struct pg_conn
270270
/* Transient state needed while establishing connection */
271271
struct addrinfo *addrlist; /* list of possible backend addresses */
272272
struct addrinfo *addr_cur; /* the one currently being tried */
273+
int addrlist_family; /* needed to know how to free addrlist */
273274
PGSetenvStatusType setenv_state; /* for 2.0 protocol only */
274275
const PQEnvironmentOption *next_eo;
275276

0 commit comments

Comments
 (0)