tzip: fix memory leak 27/170327/2
authorINSUN PYO <insun.pyo@samsung.com>
Mon, 19 Feb 2018 06:09:23 +0000 (15:09 +0900)
committerINSUN PYO <insun.pyo@samsung.com>
Mon, 19 Feb 2018 06:39:30 +0000 (15:39 +0900)
g_hash_table_inter_init() returns an iterator that should not be modified.

Links
 - https://developer.gnome.org/glib/stable/glib-Hash-Tables.html#g-hash-table-iter-init
     "Modifying the hash table after calling this function invalidates the returned iterator."

 - https://developer.gnome.org/glib/stable/glib-Hash-Tables.html#g-hash-table-iter-remove

Change-Id: I606d3309119c6911f8b3a8a9877b6f2ac5af5784
Signed-off-by: INSUN PYO <insun.pyo@samsung.com>
src/tzip/tzip-utility.c

index c50c149..45e2948 100644 (file)
@@ -252,8 +252,9 @@ static int add_dir_info(struct tzip_mount_entry *entry, const char *parent_dir,
                        return -ENOMEM;
                }
                snprintf(dinfo->name, len, "%s", parent_dir);
-               g_hash_table_insert(entry->dir_hash, dinfo->name, dinfo);
+
                fileinfo_to_stat(NULL, &dinfo->stat, S_IFDIR);
+               g_hash_table_insert(entry->dir_hash, dinfo->name, dinfo);
        }
 
        finfo = (struct tzip_file_info *)g_hash_table_lookup(
@@ -277,7 +278,6 @@ static int add_dir_info(struct tzip_mount_entry *entry, const char *parent_dir,
                strncpy(finfo->name, filename, len);
 
                fileinfo_to_stat(file_info, &finfo->stat, mode);
-
                g_hash_table_insert(dinfo->file_hash, finfo->name, finfo);
        }
 
@@ -393,7 +393,6 @@ int add_mount_entry(const char *zip_path, const char *mount_path)
                ret = -ENOMEM;
                goto out;
        }
-
        strncpy(entry->zip_path, zip_path, len);
 
        g_hash_table_insert(hashmap, entry->path, entry);
@@ -578,12 +577,12 @@ static void free_mount_node(struct tzip_mount_entry *entry)
                g_hash_table_iter_init(&f_iter, dinfo->file_hash);
                while (g_hash_table_iter_next(&f_iter, &fkey, &fval)) {
                        finfo = (struct tzip_file_info *)fval;
-                       g_hash_table_remove(dinfo->file_hash, fkey);
+                       g_hash_table_iter_remove(&f_iter);
                        free(finfo->name);
                        free(finfo);
                }
 
-               g_hash_table_remove(entry->dir_hash, dkey);
+               g_hash_table_iter_remove(&d_iter);
                free(dinfo->name);
                if (dinfo->file_hash)
                        g_hash_table_destroy(dinfo->file_hash);