Fix memory leak 24/258624/10
authorChanggyu Choi <changyu.choi@samsung.com>
Fri, 21 May 2021 07:25:51 +0000 (16:25 +0900)
committerChanggyu Choi <changyu.choi@samsung.com>
Mon, 24 May 2021 00:44:58 +0000 (09:44 +0900)
g_slist_delete_link() don't free data.
This patch add to free data.

Change-Id: I86f3647eccb58282b0340281e18e709f7cada4f8
Signed-off-by: Changgyu Choi <changyu.choi@samsung.com>
src/pkgmgrinfo_appinfo.c
src/pkgmgrinfo_pkginfo.c
src/pkgmgrinfo_private.c
src/pkgmgrinfo_private.h

index 00dc535..3a1c84b 100644 (file)
@@ -2306,8 +2306,10 @@ API int pkgmgrinfo_appinfo_filter_add_int(pkgmgrinfo_appinfo_filter_h handle,
         */
        link = g_slist_find_custom(filter->list,
                        (gconstpointer)node, __compare_func);
-       if (link)
+       if (link) {
+               _pkgmgrinfo_node_destroy(link->data);
                filter->list = g_slist_delete_link(filter->list, link);
+       }
        filter->list = g_slist_append(filter->list, (gpointer)node);
        return PMINFO_R_OK;
 
@@ -2356,8 +2358,10 @@ API int pkgmgrinfo_appinfo_filter_add_bool(pkgmgrinfo_appinfo_filter_h handle,
         */
        link = g_slist_find_custom(filter->list,
                        (gconstpointer)node, __compare_func);
-       if (link)
+       if (link) {
+               _pkgmgrinfo_node_destroy(link->data);
                filter->list = g_slist_delete_link(filter->list, link);
+       }
        filter->list = g_slist_append(filter->list, (gpointer)node);
        return PMINFO_R_OK;
 
@@ -2399,8 +2403,10 @@ API int pkgmgrinfo_appinfo_filter_add_string(pkgmgrinfo_appinfo_filter_h handle,
                node->value = strdup(value);
                link = g_slist_find_custom(filter->list,
                                (gconstpointer)node, __compare_func);
-               if (link)
+               if (link) {
+                       _pkgmgrinfo_node_destroy(link->data);
                        filter->list = g_slist_delete_link(filter->list, link);
+               }
                filter->list = g_slist_append(filter->list, (gpointer)node);
                break;
        case E_PMINFO_APPINFO_PROP_APP_CATEGORY:
@@ -2417,6 +2423,7 @@ API int pkgmgrinfo_appinfo_filter_add_string(pkgmgrinfo_appinfo_filter_h handle,
                        ptr = (pkgmgrinfo_node_x *)link->data;
                        strncpy(prev, ptr->value, PKG_STRING_LEN_MAX - 1);
                        _LOGI("Previous value is %s\n", prev);
+                       _pkgmgrinfo_node_destroy(ptr);
                        filter->list = g_slist_delete_link(filter->list, link);
                        ret = snprintf(temp, PKG_STRING_LEN_MAX - 1,
                                        "%s,%s", prev, value);
@@ -2446,8 +2453,10 @@ API int pkgmgrinfo_appinfo_filter_add_string(pkgmgrinfo_appinfo_filter_h handle,
                node->value = strndup(value, PKG_STRING_LEN_MAX - 1);
                link = g_slist_find_custom(filter->list,
                                (gconstpointer)node, __compare_func);
-               if (link)
+               if (link) {
+                       _pkgmgrinfo_node_destroy(link->data);
                        filter->list = g_slist_delete_link(filter->list, link);
+               }
                filter->list = g_slist_append(filter->list, (gpointer)node);
                break;
        }
index 4e78ca1..164f6dd 100644 (file)
@@ -71,22 +71,6 @@ static gint __pkg_disable_chk_func(gconstpointer data1, gconstpointer data2)
                return 1;
 }
 
-static void __destroy_each_node(gpointer data, gpointer user_data)
-{
-       ret_if(data == NULL);
-       pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x *)data;
-       if (node->value) {
-               free(node->value);
-               node->value = NULL;
-       }
-       if (node->key) {
-               free(node->key);
-               node->key = NULL;
-       }
-       free(node);
-       node = NULL;
-}
-
 static void __destroy_metadata_node(gpointer data)
 {
        pkgmgrinfo_metadata_node_x *node = (pkgmgrinfo_metadata_node_x *)data;
@@ -1142,12 +1126,13 @@ API int pkgmgrinfo_pkginfo_filter_destroy(pkgmgrinfo_pkginfo_filter_h handle)
 
        retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
 
-       if (filter->list) {
-               g_slist_foreach(filter->list, __destroy_each_node, NULL);
-               g_slist_free(filter->list);
-       }
+       if (filter->list)
+               g_slist_free_full(filter->list,
+                               (GDestroyNotify)_pkgmgrinfo_node_destroy);
 
-       g_slist_free_full(filter->list_metadata, __destroy_metadata_node);
+       if (filter->list_metadata)
+               g_slist_free_full(filter->list_metadata,
+                               __destroy_metadata_node);
 
        free(filter);
 
@@ -1190,8 +1175,10 @@ API int pkgmgrinfo_pkginfo_filter_add_int(pkgmgrinfo_pkginfo_filter_h handle,
        /*If API is called multiple times for same property, we should override the previous values.
        Last value set will be used for filtering.*/
        link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
-       if (link)
+       if (link) {
+               _pkgmgrinfo_node_destroy(link->data);
                filter->list = g_slist_delete_link(filter->list, link);
+       }
        filter->list = g_slist_append(filter->list, (gpointer)node);
        return PMINFO_R_OK;
 
@@ -1234,8 +1221,10 @@ API int pkgmgrinfo_pkginfo_filter_add_bool(pkgmgrinfo_pkginfo_filter_h handle,
        /*If API is called multiple times for same property, we should override the previous values.
        Last value set will be used for filtering.*/
        link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
-       if (link)
+       if (link) {
+               _pkgmgrinfo_node_destroy(link->data);
                filter->list = g_slist_delete_link(filter->list, link);
+       }
        filter->list = g_slist_append(filter->list, (gpointer)node);
        return PMINFO_R_OK;
 
@@ -1287,8 +1276,10 @@ API int pkgmgrinfo_pkginfo_filter_add_string(pkgmgrinfo_pkginfo_filter_h handle,
        /*If API is called multiple times for same property, we should override the previous values.
        Last value set will be used for filtering.*/
        link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
-       if (link)
+       if (link) {
+               _pkgmgrinfo_node_destroy(link->data);
                filter->list = g_slist_delete_link(filter->list, link);
+       }
        filter->list = g_slist_append(filter->list, (gpointer)node);
        return PMINFO_R_OK;
 
index 9291cf1..f1fc39d 100644 (file)
@@ -240,6 +240,16 @@ API pkgmgrinfo_appinfo_filter_prop_bool _pminfo_appinfo_convert_to_prop_bool(con
        return prop;
 }
 
+void _pkgmgrinfo_node_destroy(pkgmgrinfo_node_x *node)
+{
+       if (node == NULL)
+               return;
+
+       free(node->value);
+       free(node->key);
+       free(node);
+}
+
 API int __get_filter_condition(gpointer data, uid_t uid, char **condition, GList **params)
 {
        pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x *)data;
index be1bbf9..fe7170e 100644 (file)
@@ -320,6 +320,8 @@ pkgmgrinfo_appinfo_filter_prop_bool _pminfo_appinfo_convert_to_prop_bool(const c
 
 pkgmgrinfo_pkginfo_filter_prop_range _pminfo_pkginfo_convert_to_prop_range(const char *property);
 
+void _pkgmgrinfo_node_destroy(pkgmgrinfo_node_x *node);
+
 int _check_create_cert_db(void);
 void _save_column_int(sqlite3_stmt *stmt, int idx, int *i);
 void _save_column_str(sqlite3_stmt *stmt, int idx, char **str);