cpu : check whether malloc is failed 19/214919/2 accepted/tizen_5.5_unified_mobile_hotfix tizen_5.5_mobile_hotfix accepted/tizen/5.5/unified/20191031.021952 accepted/tizen/5.5/unified/mobile/hotfix/20201027.085730 accepted/tizen/unified/20190930.234525 submit/tizen/20190930.043620 submit/tizen_5.5/20191031.000004 submit/tizen_5.5_mobile_hotfix/20201026.185104 tizen_5.5.m2_release
authorKichan Kwon <k_c.kwon@samsung.com>
Fri, 27 Sep 2019 13:14:17 +0000 (22:14 +0900)
committerKichan Kwon <k_c.kwon@samsung.com>
Fri, 27 Sep 2019 13:46:42 +0000 (22:46 +0900)
Change-Id: I857aa94bb11a6aac02e9990fb556624cbc4685b2
Signed-off-by: Kichan Kwon <k_c.kwon@samsung.com>
plugin/cpu/src/bm_cpu_plugin.c

index 0637bf0..c950308 100644 (file)
@@ -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 = {