From 6ecad1072fe0d284da5f5741859177813fed3be6 Mon Sep 17 00:00:00 2001 From: Dewal Agarwal Date: Thu, 18 Jun 2020 12:27:56 +0530 Subject: [PATCH] Fix Memory Leak in AppStatus Callback Implementation - No more duplicate one-time used string for appid from GVariant - Handle memory free in both positve and fail case scenarios Change-Id: Iebc1bcf92386815fd1f57e68c4516788ad05471e --- src/battery_dump/bm_listeners.c | 66 ++++++++++++++++++++++++++++------------- 1 file changed, 46 insertions(+), 20 deletions(-) diff --git a/src/battery_dump/bm_listeners.c b/src/battery_dump/bm_listeners.c index bec0915..c13f5be 100644 --- a/src/battery_dump/bm_listeners.c +++ b/src/battery_dump/bm_listeners.c @@ -706,7 +706,6 @@ static void fill_signal_strength_change(int ss) return; } - static void fill_sleep_wakeup_change(int val) { ENTER; @@ -867,8 +866,12 @@ static int fill_app_status_change(int val, char *app_id) { ENTER; + if (app_id == NULL) + return 1; + void *prv_data = NULL; void *prv_app_id = NULL; + int error_code = 0; _INFO(" App status changed for = %s ", app_id); if (data_obj) { @@ -884,23 +887,28 @@ static int fill_app_status_change(int val, char *app_id) _INFO("event Tag creation succeeded \n"); data_obj->event_tag->sp_idx = app_hsp; if (g_hash_table_lookup_extended(app_list, app_id, &prv_app_id, &prv_data) == true) { - _INFO(" This App is already present in the list "); int *temp = (int *)prv_data; *temp = app_hsp; _INFO(" sp index for this app id is = %d and apphsp= %d", *temp, app_hsp); + error_code = 0; + goto out; } else { int *temp = (int *)calloc(1, sizeof(int)); if (temp == NULL) { _ERR("memory allocation failed"); - return 1; + BM_FREE(data_obj->event_tag); + error_code = 1; + goto out; } *temp = app_hsp; - _INFO(" This App is not present in the list , inserting it "); + _INFO("This App is not present in the list, inserting it"); g_hash_table_insert(app_list, app_id, temp); + error_code = 0; + return error_code; } } else { _ERR(" data_obj->event_tag object creation fails "); - return 1; + goto out; } } else { data_obj->event_code = ET_NONE; @@ -909,26 +917,33 @@ static int fill_app_status_change(int val, char *app_id) data_obj->event_tag = NULL; data_obj->event_tag = (history_tag_s *)calloc(1, sizeof(history_tag_s)); if (data_obj->event_tag) { - _INFO("event Tag creation succeeded \n"); - _INFO("looking for key = %s \n",app_id); + _INFO("looking for key = %s \n", app_id); if (g_hash_table_lookup_extended(app_list, app_id, &prv_app_id, &prv_data) == true) { - _INFO("found key = %s \n", (char *)prv_app_id); int *tmp = (int *)prv_data; app_hsp = *tmp; + error_code = 0; + data_obj->event_tag->sp_idx = app_hsp; + goto out; } else { _INFO(" This App is not present in the list and in background"); - return 1; + BM_FREE(data_obj->event_tag); + error_code = 1; + goto out; } - data_obj->event_tag->sp_idx = app_hsp; } else { _ERR(" data_obj->event_tag object creation fails "); - return 1; + error_code = 1; + goto out; } } } +out: + if (app_id) + g_free(app_id); + EXIT; - return 0; + return error_code; } static void fill_modem_power_state_change(int state) @@ -1808,14 +1823,17 @@ static void __app_status_signal_callback(GDBusConnection *conn, char *status; /* (issss) : pid, appid, pkgid, status, type */ - g_variant_get(params, "(is&s&s&s)", &pid, &appid, NULL, &status, NULL); + g_variant_get(params, "(i&s&s&s&s)", &pid, &appid, NULL, &status, NULL); - _DBG("pid:%d, appid:%s, status:%s", pid, appid, status); + if (status == NULL) { + _ERR("Status is NULL"); + return; + } - if (g_strcmp0(status, "fg") == 0) - val = 1; - else - val = 0; + if (appid == NULL) { + _ERR("appid is NULL"); + return; + } event_pool *app_event = (event_pool *)calloc(1, sizeof(event_pool)); if (!app_event) { @@ -1823,14 +1841,22 @@ static void __app_status_signal_callback(GDBusConnection *conn, return; } + _DBG("pid:%d, appid:%s, status:%s", pid, appid, status); + + if (g_strcmp0(status, "fg") == 0) + val = 1; + else + val = 0; + app_event->type = LISTEN_APP_STATUS; app_event->val = val; - app_event->app = appid; + app_event->app = g_strdup(appid); pthread_t producer; if (pthread_create(&producer, &attr, event_producer, app_event)) { _ERR("Failed to pthread_create."); - return; + BM_FREE(app_event->app); + BM_FREE(app_event); } EXIT; -- 2.7.4