nwrap: Fix segfaults while reloading hosts file
[obnox/samba/samba-obnox.git] / lib / nss_wrapper / nss_wrapper.c
index a8c97e1a461f3a95866f0b55a4e9abc76d9e8971..c4f1b33e4965250d8d83786a17929f2403d15dd2 100644 (file)
@@ -2622,12 +2622,14 @@ static bool nwrap_ed_inventarize_add_to_existing(struct nwrap_entdata *const ed,
        for (cursor = el; cursor->next != NULL; cursor = cursor->next)
        {
                if (cursor->ed == ed) {
-                       return false;
+                       /* The entry already exists in this list. */
+                       return true;
                }
        }
 
        if (cursor->ed == ed) {
-               return false;
+               /* The entry already exists in this list. */
+               return true;
        }
 
        el_new = nwrap_entlist_init(ed);
@@ -2888,6 +2890,7 @@ static void nwrap_he_unload(struct nwrap_cache *nwrap)
        struct nwrap_entdata *ed;
        struct nwrap_entlist *el;
        size_t i;
+       int rc;
 
        nwrap_vector_foreach (ed, nwrap_he->entries, i)
        {
@@ -2913,6 +2916,18 @@ static void nwrap_he_unload(struct nwrap_cache *nwrap)
 
        nwrap_he->num = 0;
        nwrap_he->idx = 0;
+
+       /*
+        * If we unload the file, the pointers in the hash table point to
+        * invalid memory. So we need to destroy the hash table and recreate
+        * it.
+        */
+       hdestroy();
+       rc = hcreate(max_hostents);
+       if (rc == 0) {
+               NWRAP_LOG(NWRAP_LOG_ERROR, "Failed to initialize hash table");
+               exit(-1);
+       }
 }
 
 
@@ -5577,6 +5592,11 @@ void nwrap_destructor(void)
                nwrap_he_global.num = 0;
        }
 
+       free(user_addrlist.items);
+#ifdef HAVE_GETHOSTBYNAME2
+       free(user_addrlist2.items);
+#endif
+
        hdestroy();
        NWRAP_UNLOCK_ALL;
 }