From 5131e5c92297086e120981e3bc716d896903909d Mon Sep 17 00:00:00 2001 From: jusung son Date: Tue, 19 Jun 2018 15:22:54 +0900 Subject: [PATCH] Fix memory leak Change-Id: I5c147ce3705305ca563f7c59d8f47af324c267e8 Signed-off-by: jusung son --- src/message_port_remote.c | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/message_port_remote.c b/src/message_port_remote.c index e6aaeab..4001d8d 100755 --- a/src/message_port_remote.c +++ b/src/message_port_remote.c @@ -159,12 +159,12 @@ static bool __initialize(void) } if (__sender_appid_hash == NULL) { - __sender_appid_hash = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL); + __sender_appid_hash = g_hash_table_new_full(g_str_hash, g_str_equal, free, free); retvm_if(!__sender_appid_hash, false, "fail to create __sender_appid_hash"); } if (__trusted_app_list_hash == NULL) { - __trusted_app_list_hash = g_hash_table_new(g_str_hash, g_str_equal); + __trusted_app_list_hash = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL); retvm_if(!__trusted_app_list_hash, false, "fail to create __trusted_app_list_hash"); } @@ -719,11 +719,14 @@ static void __on_sender_name_vanished(GDBusConnection *connection, const gchar *name, gpointer user_data) { - gboolean remove_result = FALSE; int *watcher_id = (int *)user_data; - remove_result = g_hash_table_remove(__sender_appid_hash, (gpointer)name); - if (!remove_result) - _LOGE("Fail to remove sender appid from hash : %s", name); + char *local_appid; + + local_appid = (char *)g_hash_table_lookup(__sender_appid_hash, name); + if (local_appid) { + g_hash_table_remove(__trusted_app_list_hash, (gpointer)local_appid); + g_hash_table_remove(__sender_appid_hash, (gpointer)name); + } if (watcher_id) { if (*watcher_id > 0) @@ -744,6 +747,7 @@ static bool __check_sender_validation(GVariant *parameters, const char *sender, int pid = __get_sender_pid(conn, sender); int *watcher_id = (int *)calloc(1, sizeof(int)); char *_sender; + char *_appid; retvm_if(!watcher_id, false, "Malloc failed"); ret = aul_app_get_appid_bypid(pid, buffer, sizeof(buffer)); @@ -768,7 +772,16 @@ static bool __check_sender_validation(GVariant *parameters, const char *sender, free(watcher_id); return false; } - g_hash_table_insert(__sender_appid_hash, (gpointer)_sender, GINT_TO_POINTER(pid)); + + _appid = strdup(local_appid); + if (_appid == NULL) { + _LOGE("out of memory"); + free(watcher_id); + free(_sender); + return false; + } + + g_hash_table_insert(__sender_appid_hash, (gpointer)_sender, (gpointer)_appid); *watcher_id = g_bus_watch_name_on_connection( gdbus_conn, sender, @@ -791,8 +804,8 @@ static void __dbus_method_call_handler(GDBusConnection *conn, gpointer user_data) { _LOGI("method_name: %s, sender: %s", method_name, sender); - gpointer sender_pid = g_hash_table_lookup(__sender_appid_hash, sender); - if (sender_pid == NULL) { + gpointer sender_appid = g_hash_table_lookup(__sender_appid_hash, sender); + if (sender_appid == NULL) { if (!__check_sender_validation(parameters, sender, conn)) goto out; } -- 2.7.4