]> granicus.if.org Git - postgresql/commitdiff
Fix minor memory leak in ident_inet().
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 12 Feb 2015 00:09:54 +0000 (19:09 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 12 Feb 2015 00:09:54 +0000 (19:09 -0500)
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.

src/backend/libpq/auth.c

index 2a67bcd9926832ca0b856998306d9d0aab5811c1..d5260f47849f0b3de5b15cb073699f2d014d4c2b 100644 (file)
@@ -1403,8 +1403,7 @@ ident_inet(hbaPort *port)
        const SockAddr remote_addr = port->raddr;
        const SockAddr local_addr = port->laddr;
        char            ident_user[IDENT_USERNAME_MAX + 1];
-       pgsocket        sock_fd;                /* File descriptor for socket on which we talk
-                                                                * to Ident */
+       pgsocket        sock_fd = PGINVALID_SOCKET;             /* for talking to Ident server */
        int                     rc;                             /* Return code from a locally called function */
        bool            ident_return;
        char            remote_addr_s[NI_MAXHOST];
@@ -1443,9 +1442,9 @@ ident_inet(hbaPort *port)
        rc = pg_getaddrinfo_all(remote_addr_s, ident_port, &hints, &ident_serv);
        if (rc || !ident_serv)
        {
-               if (ident_serv)
-                       pg_freeaddrinfo_all(hints.ai_family, ident_serv);
-               return STATUS_ERROR;    /* we don't expect this to happen */
+               /* we don't expect this to happen */
+               ident_return = false;
+               goto ident_inet_done;
        }
 
        hints.ai_flags = AI_NUMERICHOST;
@@ -1459,9 +1458,9 @@ ident_inet(hbaPort *port)
        rc = pg_getaddrinfo_all(local_addr_s, NULL, &hints, &la);
        if (rc || !la)
        {
-               if (la)
-                       pg_freeaddrinfo_all(hints.ai_family, la);
-               return STATUS_ERROR;    /* we don't expect this to happen */
+               /* we don't expect this to happen */
+               ident_return = false;
+               goto ident_inet_done;
        }
 
        sock_fd = socket(ident_serv->ai_family, ident_serv->ai_socktype,
@@ -1548,8 +1547,10 @@ ident_inet(hbaPort *port)
 ident_inet_done:
        if (sock_fd != PGINVALID_SOCKET)
                closesocket(sock_fd);
-       pg_freeaddrinfo_all(remote_addr.addr.ss_family, ident_serv);
-       pg_freeaddrinfo_all(local_addr.addr.ss_family, la);
+       if (ident_serv)
+               pg_freeaddrinfo_all(remote_addr.addr.ss_family, ident_serv);
+       if (la)
+               pg_freeaddrinfo_all(local_addr.addr.ss_family, la);
 
        if (ident_return)
                /* Success! Check the usermap */