]> git.ipfire.org Git - thirdparty/krb5.git/commitdiff
Fix minor leak in k5_os_hostaddr() 794/head
authorGreg Hudson <ghudson@mit.edu>
Fri, 15 Jun 2018 15:31:04 +0000 (11:31 -0400)
committerGreg Hudson <ghudson@mit.edu>
Tue, 19 Jun 2018 14:12:12 +0000 (10:12 -0400)
In k5_os_hostaddr(), if allocation of the result array fails, use the
cleanup handler so that the getaddrinfo() result is freed.  Also
initialize the pointers which are freed in the cleanup handler for
safety.  Reported by Bean Zhang.

ticket: 8699

src/lib/krb5/os/hostaddr.c

index d7a4a763012d6c93719ddda2498ebb80cbb42ff9..129a4adcf6e0fb00b8df82279cf558670426bc77 100644 (file)
@@ -34,9 +34,9 @@ k5_os_hostaddr(krb5_context context, const char *name,
                krb5_address ***ret_addrs)
 {
     krb5_error_code     retval;
-    krb5_address        **addrs;
+    krb5_address        **addrs = NULL;
     int                 i, j, r;
-    struct addrinfo hints, *ai, *aip;
+    struct addrinfo hints, *ai = NULL, *aip;
 
     if (!name)
         return KRB5_ERR_BAD_HOSTNAME;
@@ -68,9 +68,9 @@ k5_os_hostaddr(krb5_context context, const char *name,
         }
     }
 
-    addrs = malloc ((i+1) * sizeof(*addrs));
-    if (!addrs)
-        return ENOMEM;
+    addrs = k5calloc(i + 1, sizeof(*addrs), &retval);
+    if (addrs == NULL)
+        goto errout;
 
     for (j = 0; j < i + 1; j++)
         addrs[j] = 0;