Fix svace and coverty memleak issues 09/219509/6
authorSudipto <sudipto.bal@samsung.com>
Thu, 5 Dec 2019 10:59:53 +0000 (16:29 +0530)
committerSudipto Bal <sudipto.bal@samsung.com>
Thu, 19 Dec 2019 09:44:22 +0000 (09:44 +0000)
Change-Id: I862f2ccb659bb160c7cfb15af385c5640d8f8844
Signed-off-by: Sudipto <sudipto.bal@samsung.com>
plugin/bm-bt-plugin/src/bm_bt_plugin.c

index 50f4de5..cf376d3 100644 (file)
@@ -80,6 +80,18 @@ int deinit()
        return BM_PLUGIN_ERROR_NONE;
 }
 
+static void free_app_list(GSList *app_list)
+{
+       GSList *l = NULL;
+       app_time_map_st2 *app_info;
+       for (l = app_list; l != NULL; l = g_slist_next(l)){
+               app_info = (app_time_map_st2 *)(l->data);
+               if (app_info != NULL)
+                       free(app_info->app_id);
+       }
+       g_slist_free(app_list);
+}
+
 int get_feature_data(bm_data_h *handle, bm_plugin_data_type_e type)
 {
        ENTER;
@@ -88,13 +100,7 @@ int get_feature_data(bm_data_h *handle, bm_plugin_data_type_e type)
        bm_bluetooth_st *bluetooth_record = NULL;
        app_time_map_st2 *app_record = NULL;
 
-       bluetooth_record_head = (bm_bluetooth_st *)calloc(1, sizeof(bm_bluetooth_st));
-       if (bluetooth_record_head == NULL) {
-               _E("Calloc Failed");
-               return BM_PLUGIN_ERROR_OUT_OF_MEMORY;
-       }
-
-       bluetooth_record = (bm_bluetooth_st*)calloc(1, sizeof(bm_bluetooth_st));
+       bluetooth_record = (bm_bluetooth_st *)calloc(1, sizeof(bm_bluetooth_st));
        if (bluetooth_record == NULL) {
                _E("Calloc Failed");
                return BM_PLUGIN_ERROR_OUT_OF_MEMORY;
@@ -108,6 +114,7 @@ int get_feature_data(bm_data_h *handle, bm_plugin_data_type_e type)
        if (ret != 0)
        {
                _E("battery info not read");
+               free(bluetooth_record);
                return BM_PLUGIN_ERROR_NO_DATA;
        }
 
@@ -125,19 +132,23 @@ int get_feature_data(bm_data_h *handle, bm_plugin_data_type_e type)
        /* Getting app data */
        GSList *app_list = NULL, *l = NULL;
        app_list = bluetooth_record->atm_list;
-       bt_battery_app_info_s *app_info;
+       bt_battery_app_info_s *app_info = NULL;
 
        for (l = battery_data.atm_list; l != NULL; l = g_slist_next(l)) {
 
-               app_record = (app_time_map_st2*)calloc(1, sizeof(app_time_map_st2));
+               app_record = (app_time_map_st2 *)calloc(1, sizeof(app_time_map_st2));
                if (app_record == NULL) {
                        _E("Calloc Failed");
+                       free_app_list(bluetooth_record->atm_list);
+                       free(bluetooth_record);
                        return BM_PLUGIN_ERROR_OUT_OF_MEMORY;
                }
 
                app_info = (bt_battery_app_info_s *)(l->data);
                if (app_info == NULL) {
                        _E("app_info is NULL");
+                       free_app_list(bluetooth_record->atm_list);
+                       free(bluetooth_record);
                        return BM_PLUGIN_ERROR_NO_DATA;
                }
 
@@ -152,6 +163,13 @@ int get_feature_data(bm_data_h *handle, bm_plugin_data_type_e type)
        }
 
        /*Append BT data to head node as per design doc*/
+       bluetooth_record_head = (bm_bluetooth_st *)calloc(1, sizeof(bm_bluetooth_st));
+       if (bluetooth_record_head == NULL) {
+               _E("Calloc Failed");
+               free_app_list(bluetooth_record->atm_list);
+               free(bluetooth_record);
+               return BM_PLUGIN_ERROR_OUT_OF_MEMORY;
+       }
        bluetooth_record_head->bt_data_list = g_slist_append(
                                bluetooth_record_head->bt_data_list, bluetooth_record);
        *handle = bluetooth_record_head;