Skip to content

Commit 53747f7

Browse files
committed
libpq: Add pqReleaseConnHosts function
In a follow up commit we'll need to free this connhost field in a function defined in fe-cancel.c, so here we extract the logic to a dedicated extern function. Author: Jelte Fennema-Nio <jelte.fennema@microsoft.com> Discussion: https://postgr.es/m/AM5PR83MB0178D3B31CA1B6EC4A8ECC42F7529@AM5PR83MB0178.EURPRD83.prod.outlook.com
1 parent c717525 commit 53747f7

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

src/interfaces/libpq/fe-connect.c

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4349,19 +4349,7 @@ freePGconn(PGconn *conn)
43494349
free(conn->events[i].name);
43504350
}
43514351

4352-
/* clean up pg_conn_host structures */
4353-
for (int i = 0; i < conn->nconnhost; ++i)
4354-
{
4355-
free(conn->connhost[i].host);
4356-
free(conn->connhost[i].hostaddr);
4357-
free(conn->connhost[i].port);
4358-
if (conn->connhost[i].password != NULL)
4359-
{
4360-
explicit_bzero(conn->connhost[i].password, strlen(conn->connhost[i].password));
4361-
free(conn->connhost[i].password);
4362-
}
4363-
}
4364-
free(conn->connhost);
4352+
pqReleaseConnHosts(conn);
43654353

43664354
free(conn->client_encoding_initial);
43674355
free(conn->events);
@@ -4423,6 +4411,31 @@ freePGconn(PGconn *conn)
44234411
free(conn);
44244412
}
44254413

4414+
/*
4415+
* pqReleaseConnHosts
4416+
* - Free the host list in the PGconn.
4417+
*/
4418+
void
4419+
pqReleaseConnHosts(PGconn *conn)
4420+
{
4421+
if (conn->connhost)
4422+
{
4423+
for (int i = 0; i < conn->nconnhost; ++i)
4424+
{
4425+
free(conn->connhost[i].host);
4426+
free(conn->connhost[i].hostaddr);
4427+
free(conn->connhost[i].port);
4428+
if (conn->connhost[i].password != NULL)
4429+
{
4430+
explicit_bzero(conn->connhost[i].password,
4431+
strlen(conn->connhost[i].password));
4432+
free(conn->connhost[i].password);
4433+
}
4434+
}
4435+
free(conn->connhost);
4436+
}
4437+
}
4438+
44264439
/*
44274440
* store_conn_addrinfo
44284441
* - copy addrinfo to PGconn object

src/interfaces/libpq/libpq-int.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,7 @@ extern void pqDropConnection(PGconn *conn, bool flushInput);
678678
#if defined(WIN32) && defined(SIO_KEEPALIVE_VALS)
679679
extern int pqSetKeepalivesWin32(pgsocket sock, int idle, int interval);
680680
#endif
681+
extern void pqReleaseConnHosts(PGconn *conn);
681682
extern int pqPacketSend(PGconn *conn, char pack_type,
682683
const void *buf, size_t buf_len);
683684
extern bool pqGetHomeDirectory(char *buf, int bufsize);

0 commit comments

Comments
 (0)