Skip to content

Commit 0168fb0

Browse files
committed
Fix minor memory leak in ident_inet().
We'd leak the ident_serv data structure if the second pg_getaddrinfo_all (the one for the local address) failed. This is not of great consequence because a failure return here just leads directly to backend exit(), but if this function is going to try to clean up after itself at all, it should not have such holes in the logic. Try to fix it in a future-proof way by having all the failure exits go through the same cleanup path, rather than "optimizing" some of them. Per Coverity. Back-patch to 9.2, which is as far back as this patch applies cleanly.
1 parent 8b63f89 commit 0168fb0

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

src/backend/libpq/auth.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1613,8 +1613,7 @@ ident_inet(hbaPort *port)
16131613
const SockAddr remote_addr = port->raddr;
16141614
const SockAddr local_addr = port->laddr;
16151615
char ident_user[IDENT_USERNAME_MAX + 1];
1616-
pgsocket sock_fd; /* File descriptor for socket on which we talk
1617-
* to Ident */
1616+
pgsocket sock_fd = PGINVALID_SOCKET; /* for talking to Ident server */
16181617
int rc; /* Return code from a locally called function */
16191618
bool ident_return;
16201619
char remote_addr_s[NI_MAXHOST];
@@ -1653,9 +1652,9 @@ ident_inet(hbaPort *port)
16531652
rc = pg_getaddrinfo_all(remote_addr_s, ident_port, &hints, &ident_serv);
16541653
if (rc || !ident_serv)
16551654
{
1656-
if (ident_serv)
1657-
pg_freeaddrinfo_all(hints.ai_family, ident_serv);
1658-
return STATUS_ERROR; /* we don't expect this to happen */
1655+
/* we don't expect this to happen */
1656+
ident_return = false;
1657+
goto ident_inet_done;
16591658
}
16601659

16611660
hints.ai_flags = AI_NUMERICHOST;
@@ -1669,9 +1668,9 @@ ident_inet(hbaPort *port)
16691668
rc = pg_getaddrinfo_all(local_addr_s, NULL, &hints, &la);
16701669
if (rc || !la)
16711670
{
1672-
if (la)
1673-
pg_freeaddrinfo_all(hints.ai_family, la);
1674-
return STATUS_ERROR; /* we don't expect this to happen */
1671+
/* we don't expect this to happen */
1672+
ident_return = false;
1673+
goto ident_inet_done;
16751674
}
16761675

16771676
sock_fd = socket(ident_serv->ai_family, ident_serv->ai_socktype,
@@ -1758,8 +1757,10 @@ ident_inet(hbaPort *port)
17581757
ident_inet_done:
17591758
if (sock_fd != PGINVALID_SOCKET)
17601759
closesocket(sock_fd);
1761-
pg_freeaddrinfo_all(remote_addr.addr.ss_family, ident_serv);
1762-
pg_freeaddrinfo_all(local_addr.addr.ss_family, la);
1760+
if (ident_serv)
1761+
pg_freeaddrinfo_all(remote_addr.addr.ss_family, ident_serv);
1762+
if (la)
1763+
pg_freeaddrinfo_all(local_addr.addr.ss_family, la);
17631764

17641765
if (ident_return)
17651766
/* Success! Check the usermap */

0 commit comments

Comments
 (0)