opkg: (leak fixing, day 3) fixed final memory leaks fixed reported from
[oweals/opkg-lede.git] / libopkg / pkg_hash.c
index 0106e6550740493ddbf1391feb62de6c6491d042..2fb0d1d8bf4a44639816f11a057ed449326aa73f 100644 (file)
@@ -1,4 +1,4 @@
-/* opkg_hash.c - the itsy package management system
+/* opkg_hash.c - the opkg package management system
 
    Steven M. Ayer
    
@@ -15,7 +15,7 @@
    General Public License for more details.
 */
 
-#include "opkg.h"
+#include "includes.h"
 #include <errno.h>
 #include <ctype.h>
 #include <stdlib.h>
@@ -53,8 +53,36 @@ int pkg_hash_init(const char *name, hash_table_t *hash, int len)
   return hash_table_init(name, hash, len);
 }
 
+void free_pkgs (const char *key, void *entry, void *data)
+{
+  int i;
+  abstract_pkg_t *ab_pkg;
+
+  /* each entry in the hash table is an abstract package, which contains a list
+   * of packages that provide the abstract package */
+  
+  ab_pkg = (abstract_pkg_t*) entry;
+
+  if (ab_pkg->pkgs)
+  {
+    for (i = 0; i < ab_pkg->pkgs->len; i++)
+    {
+      pkg_deinit (ab_pkg->pkgs->pkgs[i]);
+      free (ab_pkg->pkgs->pkgs[i]);
+    }
+  }
+
+  abstract_pkg_vec_free (ab_pkg->provided_by);
+  abstract_pkg_vec_free (ab_pkg->replaced_by);
+  pkg_vec_free (ab_pkg->pkgs);
+  free (ab_pkg->depended_upon_by);
+  free (ab_pkg->name);
+  free (ab_pkg);
+}
+
 void pkg_hash_deinit(hash_table_t *hash)
 {
+  hash_table_foreach (hash, free_pkgs, NULL);
   hash_table_deinit(hash);
 }
 
@@ -112,6 +140,7 @@ int pkg_hash_add_from_file(opkg_conf_t *conf, const char *file_name,
               }
               hash_insert_pkg(hash, pkg, is_status_file,conf);
          } else {
+              pkg_deinit (pkg);
               free(pkg);
          }
      }