From 544de1f97d10249c4d47f9d01f76431f82ea0ac8 Mon Sep 17 00:00:00 2001 From: Seonah Moon Date: Mon, 18 Mar 2019 18:09:13 +0900 Subject: [PATCH] Fix memory leak Change-Id: I4c16e3d4db431e0b24359bb21694c56403fd1c36 --- packaging/download-provider.spec | 2 +- provider-interface/download-provider-interface.c | 36 ++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/packaging/download-provider.spec b/packaging/download-provider.spec index 7b84e56..56e6a85 100755 --- a/packaging/download-provider.spec +++ b/packaging/download-provider.spec @@ -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 diff --git a/provider-interface/download-provider-interface.c b/provider-interface/download-provider-interface.c index c548bfa..1918778 100755 --- a/provider-interface/download-provider-interface.c +++ b/provider-interface/download-provider-interface.c @@ -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) -- 2.7.4