From 9df2f55df481b41bc6c29d0643dac5d1e18312b1 Mon Sep 17 00:00:00 2001 From: Kichan Kwon Date: Fri, 27 Sep 2019 22:14:17 +0900 Subject: [PATCH] cpu : check whether malloc is failed Change-Id: I857aa94bb11a6aac02e9990fb556624cbc4685b2 Signed-off-by: Kichan Kwon --- plugin/cpu/src/bm_cpu_plugin.c | 51 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/plugin/cpu/src/bm_cpu_plugin.c b/plugin/cpu/src/bm_cpu_plugin.c index 0637bf0..c950308 100644 --- a/plugin/cpu/src/bm_cpu_plugin.c +++ b/plugin/cpu/src/bm_cpu_plugin.c @@ -56,6 +56,18 @@ gint find_app_time(gconstpointer a, gconstpointer b) return g_strcmp0(appid, app_time->app_id); } +void free_atm_st1(gpointer data) +{ + app_time_map_st1 *atm = (app_time_map_st1 *)data; + + if (!atm) + return; + + if (atm->app_id) + free(atm->app_id); + free(atm); +} + bool is_running_app(const char *appid) { bool is_running = false; @@ -242,6 +254,11 @@ int get_feature_data(bm_data_h *handle, bm_plugin_data_type_e type) if (!app_status) { _D("%s is newly launched", appid); new_app_status = malloc(sizeof(struct app_status)); + if (!new_app_status) { + _E("malloc failed"); + ret = BM_PLUGIN_ERROR_OUT_OF_MEMORY; + goto clean_atm; + } new_app_status->pid = pid; new_app_status->last_checkpoint = time; new_app_status->used_time = utime + stime; @@ -283,7 +300,17 @@ int get_feature_data(bm_data_h *handle, bm_plugin_data_type_e type) /* Insert the app using CPU in the ATM list */ if (!g_slist_find_custom(usage->atm_list, appid, find_app_time)) { app_time_map = malloc(sizeof(app_time_map_st1)); + if (!app_time_map) { + _E("malloc failed"); + ret = BM_PLUGIN_ERROR_OUT_OF_MEMORY; + goto clean_atm; + } app_time_map->app_id = g_strdup(appid); + if (!app_time_map->app_id) { + _E("g_strdup failed"); + ret = BM_PLUGIN_ERROR_OUT_OF_MEMORY; + goto clean_atm; + } app_time_map->time = tv.tv_sec - app_status->last_checkpoint; if (app_time_map->time <= 0) app_time_map->time = 1; @@ -294,8 +321,6 @@ int get_feature_data(bm_data_h *handle, bm_plugin_data_type_e type) app_status->used_time = utime + stime; app_status->last_status = status; } - sqlite3_finalize(stmt); - sqlite3_close(db); /* Update all running apps with current value */ g_hash_table_iter_init(&iter, running_app_list); @@ -342,7 +367,17 @@ int get_feature_data(bm_data_h *handle, bm_plugin_data_type_e type) /* Insert the app using CPU in the ATM list */ if (!g_slist_find_custom(usage->atm_list, appid, find_app_time)) { app_time_map = malloc(sizeof(app_time_map_st1)); + if (!app_time_map) { + _E("malloc failed"); + ret = BM_PLUGIN_ERROR_OUT_OF_MEMORY; + goto clean_atm; + } app_time_map->app_id = g_strdup(appid); + if (!app_time_map->app_id) { + _E("g_strdup failed"); + ret = BM_PLUGIN_ERROR_OUT_OF_MEMORY; + goto clean_atm; + } app_time_map->time = tv.tv_sec - last_requested_time.tv_sec; if (app_time_map->time <= 0) app_time_map->time = 1; @@ -362,8 +397,18 @@ int get_feature_data(bm_data_h *handle, bm_plugin_data_type_e type) *handle = usage; _I("Succeed to get CPU usage"); + ret = BM_PLUGIN_ERROR_NONE; + goto clean_db; + +clean_atm: + g_slist_free_full(usage->atm_list, free_atm_st1); + +clean_db: + sqlite3_finalize(stmt); + sqlite3_close(db); + EXIT; - return BM_PLUGIN_ERROR_NONE; + return ret; } static bm_api_st cpu_api = { -- 2.7.4