Skip to content

Commit bcf2dec

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 5ea8cfe commit bcf2dec

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
@@ -1616,8 +1616,7 @@ ident_inet(hbaPort *port)
16161616
const SockAddr remote_addr = port->raddr;
16171617
const SockAddr local_addr = port->laddr;
16181618
char ident_user[IDENT_USERNAME_MAX + 1];
1619-
pgsocket sock_fd; /* File descriptor for socket on which we talk
1620-
* to Ident */
1619+
pgsocket sock_fd = PGINVALID_SOCKET; /* for talking to Ident server */
16211620
int rc; /* Return code from a locally called function */
16221621
bool ident_return;
16231622
char remote_addr_s[NI_MAXHOST];
@@ -1656,9 +1655,9 @@ ident_inet(hbaPort *port)
16561655
rc = pg_getaddrinfo_all(remote_addr_s, ident_port, &hints, &ident_serv);
16571656
if (rc || !ident_serv)
16581657
{
1659-
if (ident_serv)
1660-
pg_freeaddrinfo_all(hints.ai_family, ident_serv);
1661-
return STATUS_ERROR; /* we don't expect this to happen */
1658+
/* we don't expect this to happen */
1659+
ident_return = false;
1660+
goto ident_inet_done;
16621661
}
16631662

16641663
hints.ai_flags = AI_NUMERICHOST;
@@ -1672,9 +1671,9 @@ ident_inet(hbaPort *port)
16721671
rc = pg_getaddrinfo_all(local_addr_s, NULL, &hints, &la);
16731672
if (rc || !la)
16741673
{
1675-
if (la)
1676-
pg_freeaddrinfo_all(hints.ai_family, la);
1677-
return STATUS_ERROR; /* we don't expect this to happen */
1674+
/* we don't expect this to happen */
1675+
ident_return = false;
1676+
goto ident_inet_done;
16781677
}
16791678

16801679
sock_fd = socket(ident_serv->ai_family, ident_serv->ai_socktype,
@@ -1761,8 +1760,10 @@ ident_inet(hbaPort *port)
17611760
ident_inet_done:
17621761
if (sock_fd != PGINVALID_SOCKET)
17631762
closesocket(sock_fd);
1764-
pg_freeaddrinfo_all(remote_addr.addr.ss_family, ident_serv);
1765-
pg_freeaddrinfo_all(local_addr.addr.ss_family, la);
1763+
if (ident_serv)
1764+
pg_freeaddrinfo_all(remote_addr.addr.ss_family, ident_serv);
1765+
if (la)
1766+
pg_freeaddrinfo_all(local_addr.addr.ss_family, la);
17661767

17671768
if (ident_return)
17681769
/* Success! Check the usermap */

0 commit comments

Comments
 (0)