nwrap: Fix memory leak in nwrap_files_gethostbyname()
authorRobin Hack <hack.robin@gmail.com>
Thu, 8 Oct 2015 13:27:47 +0000 (15:27 +0200)
committerMichael Adam <obnox@samba.org>
Mon, 11 Jan 2016 11:25:28 +0000 (12:25 +0100)
Signed-off-by: Robin Hack <hack.robin@gmail.com>
Reviewed-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Michael Adam <obnox@samba.org>
lib/nss_wrapper/nss_wrapper.c

index 3496162aad69b2f78ce576d12def8fcf03b68a9c..970b5b00add975d63ce326ef3ab6b1e0bf97aad3 100644 (file)
@@ -604,6 +604,8 @@ struct nwrap_vector {
             item != NULL; \
             (item) = (vect).items[++iter])
 
+#define nwrap_vector_is_initialized(vector) ((vector)->items != NULL)
+
 static inline bool nwrap_vector_init(struct nwrap_vector *const vector)
 {
        if (vector == NULL) {
@@ -3323,10 +3325,16 @@ static int nwrap_files_gethostbyname(const char *name, int af,
        SAFE_FREE(h_name_lower);
 
        /* Always cleanup vector and results */
-       if (!nwrap_vector_init(addr_list)) {
-               NWRAP_LOG(NWRAP_LOG_DEBUG,
-                         "Unable to initialize memory for addr_list vector");
-               goto no_ent;
+       if (!nwrap_vector_is_initialized(addr_list)) {
+               if (!nwrap_vector_init(addr_list)) {
+                       NWRAP_LOG(NWRAP_LOG_DEBUG,
+                                 "Unable to initialize memory for addr_list vector");
+                       goto no_ent;
+               }
+       } else {
+               /* When vector is initialized data are valid no more.
+                * Quick way how to free vector is: */
+               addr_list->count = 0;
        }
 
        /* Iterate through results */