Fix memory leak 39/201639/2 accepted/tizen/unified/20190320.082310 submit/tizen/20190319.071352
authorSeonah Moon <seonah1.moon@samsung.com>
Mon, 18 Mar 2019 09:09:13 +0000 (18:09 +0900)
committerSeonah Moon <seonah1.moon@samsung.com>
Tue, 19 Mar 2019 06:27:16 +0000 (15:27 +0900)
Change-Id: I4c16e3d4db431e0b24359bb21694c56403fd1c36

packaging/download-provider.spec
provider-interface/download-provider-interface.c

index 7b84e56..56e6a85 100755 (executable)
@@ -1,6 +1,6 @@
 Name:       download-provider
 Summary:    Download the contents in background
-Version:    2.1.109
+Version:    2.1.110
 Release:    0
 Group:      Development/Libraries
 License:    Apache-2.0
index c548bfa..1918778 100755 (executable)
@@ -785,6 +785,16 @@ int dp_interface_destroy(const int id)
                g_interface_slots[index].state_data = NULL;
                g_interface_slots[index].progress = NULL;
                g_interface_slots[index].progress_data = NULL;
+
+               if (g_interface_slots[index].saved_temp_path) {
+                       free(g_interface_slots[index].saved_temp_path);
+                       g_interface_slots[index].saved_temp_path = NULL;
+               }
+
+               if (g_interface_slots[index].downloaded_path) {
+                       free(g_interface_slots[index].downloaded_path);
+                       g_interface_slots[index].downloaded_path = NULL;
+               }
        }
        CLIENT_MUTEX_UNLOCK(&g_function_mutex);
        return __dp_ipc_echo(id, DP_SEC_CONTROL, DP_PROP_DESTROY, __FUNCTION__);
@@ -822,6 +832,7 @@ int dp_interface_set_destination(const int id, const char *path)
        char set_filename[DOWNLOAD_FILENAME_MAX] = {0, };
        char *resolved_path = NULL;
        int index = __get_my_slot_index(id);
+       int ret = DOWNLOAD_ADAPTOR_ERROR_NONE;
 
        if (index < 0)
                return DOWNLOAD_ADAPTOR_ERROR_INVALID_PARAMETER;
@@ -829,11 +840,14 @@ int dp_interface_set_destination(const int id, const char *path)
        if (path == NULL)
                return __dp_ipc_echo(id, DP_SEC_UNSET, DP_PROP_DESTINATION, __FUNCTION__);
 
+       CLIENT_MUTEX_LOCK(&g_function_mutex);
        if (g_interface_slots[index].downloaded_path)
                free(g_interface_slots[index].downloaded_path);
 
        g_interface_slots[index].downloaded_path = strdup(path);
        resolved_path = realpath(g_interface_slots[index].downloaded_path, NULL);
+       CLIENT_MUTEX_UNLOCK(&g_function_mutex);
+
        if (!resolved_path) {
                TRACE_ERROR("Invalid path");
                return DOWNLOAD_ADAPTOR_ERROR_INVALID_PARAMETER;
@@ -844,11 +858,14 @@ int dp_interface_set_destination(const int id, const char *path)
                /* Converted. Use converted path. */
                TRACE_ERROR("Converted filename : %s -> %s",
                                resolved_path, set_filename);
+               free(resolved_path);
                return __dp_ipc_set_string(id, DP_SEC_SET,
                                DP_PROP_DESTINATION, set_filename, __FUNCTION__);
        }
-       return __dp_ipc_set_string(id, DP_SEC_SET,
+       ret = __dp_ipc_set_string(id, DP_SEC_SET,
                        DP_PROP_DESTINATION, resolved_path, __FUNCTION__);
+       free(resolved_path);
+       return ret;
 }
 
 int dp_interface_get_destination(const int id, char **path)
@@ -858,12 +875,16 @@ int dp_interface_get_destination(const int id, char **path)
        if (index < 0 || !path)
                return DOWNLOAD_ADAPTOR_ERROR_INVALID_PARAMETER;
 
+       CLIENT_MUTEX_LOCK(&g_function_mutex);
        if (!g_interface_slots[index].downloaded_path) {
                *path = NULL;
+               CLIENT_MUTEX_UNLOCK(&g_function_mutex);
                return DOWNLOAD_ADAPTOR_ERROR_NO_DATA;
        }
 
        *path = strdup(g_interface_slots[index].downloaded_path);
+       CLIENT_MUTEX_UNLOCK(&g_function_mutex);
+
        if (*path == NULL)
                return DOWNLOAD_ADAPTOR_ERROR_OUT_OF_MEMORY;
 
@@ -911,7 +932,9 @@ int dp_interface_get_temp_path(const int id, char **temp_path)
        if (index < 0 || !temp_path)
                return DOWNLOAD_ADAPTOR_ERROR_INVALID_PARAMETER;
 
+       CLIENT_MUTEX_LOCK(&g_function_mutex);
        if (!g_interface_slots[index].saved_temp_path) {
+               CLIENT_MUTEX_UNLOCK(&g_function_mutex);
                ret = __dp_ipc_get_string(id, DP_PROP_TEMP_SAVED_PATH, temp_path, __FUNCTION__);
                if (storage_get_compat_internal_path(*temp_path, DOWNLOAD_FILENAME_MAX, compat_filename) == 0) {
                        /* Converted. Use converted path. */
@@ -920,8 +943,10 @@ int dp_interface_get_temp_path(const int id, char **temp_path)
                }
        } else {
                *temp_path = strdup(g_interface_slots[index].saved_temp_path);
+               CLIENT_MUTEX_UNLOCK(&g_function_mutex);
        }
 
+
        if (*temp_path == NULL)
                ret = DOWNLOAD_ADAPTOR_ERROR_OUT_OF_MEMORY;
 
@@ -943,15 +968,19 @@ int dp_interface_set_temp_file_path(const int id, const char *path)
        char set_filename[DOWNLOAD_FILENAME_MAX] = {0, };
        char *resolved_path = NULL;
        int index = __get_my_slot_index(id);
+       int ret = DOWNLOAD_ADAPTOR_ERROR_NONE;
 
        if (index < 0)
                return DOWNLOAD_ADAPTOR_ERROR_INVALID_PARAMETER;
 
+       CLIENT_MUTEX_LOCK(&g_function_mutex);
        if (g_interface_slots[index].saved_temp_path)
                free(g_interface_slots[index].saved_temp_path);
 
        g_interface_slots[index].saved_temp_path = strdup(path);
        resolved_path = realpath(g_interface_slots[index].saved_temp_path, NULL);
+       CLIENT_MUTEX_UNLOCK(&g_function_mutex);
+
        if (!resolved_path) {
                TRACE_ERROR("Invalid path");
                return DOWNLOAD_ADAPTOR_ERROR_INVALID_PARAMETER;
@@ -961,12 +990,15 @@ int dp_interface_set_temp_file_path(const int id, const char *path)
                        /* Converted. Use converted path. */
                        TRACE_ERROR("Converted filename : %s -> %s",
                                        resolved_path, set_filename);
+                       free(resolved_path);
                        return __dp_ipc_set_string(id, DP_SEC_SET,
                                        DP_PROP_TEMP_SAVED_PATH, set_filename, __FUNCTION__);
                }
        }
-       return __dp_ipc_set_string(id, DP_SEC_SET,
+       ret = __dp_ipc_set_string(id, DP_SEC_SET,
                        DP_PROP_TEMP_SAVED_PATH, resolved_path, __FUNCTION__);
+       free(resolved_path);
+       return ret;
 }
 
 int dp_interface_set_network_type(const int id, int net_type)