[Non-ACR] Fixing memory issues 83/229183/1
authorAbhishek Vijay <abhishek.v@samsung.com>
Mon, 30 Mar 2020 05:27:00 +0000 (10:57 +0530)
committerAbhishek Vijay <abhishek.v@samsung.com>
Mon, 30 Mar 2020 07:12:19 +0000 (07:12 +0000)
Change-Id: Id05f44d81b41e68cb3043235106b990e08bc005f
Signed-off-by: Abhishek Vijay <abhishek.v@samsung.com>
src/battery_dump/bd_history_item.c
src/bm_server_db.c

index 6ff59c6..3c443b0 100644 (file)
@@ -51,6 +51,7 @@ GHashTable *app_map = NULL;
 static int bd_print_pool_entry(int fd, int idx, history_tag_s *pool_info)
 {
        ENTER;
+
        BM_CHECK_INPUT_PARAM(pool_info);
        _DBG("Value from history data %d", idx);
 
@@ -73,10 +74,13 @@ static int bd_print_pool_entry(int fd, int idx, history_tag_s *pool_info)
 
        g_string_append(dump_p, "\"");
        g_string_append(dump_p, "\n");
+
        _DBG("%s", dump_p->str);
+
        if (write(fd, dump_p->str, dump_p->len) < 0)
                _WARN("write error");
-       g_free(dump_p->str);
+
+       g_string_free(dump_p, TRUE);
 
        EXIT;
        return BATTERY_MONITOR_ERROR_NONE;
@@ -88,16 +92,19 @@ static int bd_get_pool_entry_from_dump(int fd)
 
        GString *dump_p = g_string_sized_new(30);
        BM_CHECK_MEM_ALLOC(dump_p, {});
+
        g_string_append(dump_p, TIZEN_DUMP_VERSION);
        g_string_append(dump_p, ",0");
        g_string_append(dump_p, ",i");
        g_string_append(dump_p, ",vers");
        g_string_append(dump_p, ",19,150,NMF26V,NMF26V");
        g_string_append(dump_p, "\n");
+
        _DBG("%s", dump_p->str);
+
        if (write(fd, dump_p->str, dump_p->len) < 0)
                _WARN("write error");
-       g_free(dump_p->str);
+       g_string_free(dump_p, TRUE);
 
        int ret = BATTERY_MONITOR_ERROR_NONE;
        int log_count = h_count; int pool_count = 0;
@@ -161,7 +168,7 @@ static int bd_get_and_print_battery_usage_from_dump(int fd, long int duration)
        _DBG("%s", dump_p->str);
        if (write(fd, dump_p->str, dump_p->len) < 0)
                _WARN("write error");
-       g_free(dump_p->str);
+       g_string_free(dump_p, TRUE);
 
        g_hash_table_iter_init(&iter, app_map);
        while (g_hash_table_iter_next(&iter, &key, &value)) {
@@ -189,7 +196,7 @@ static int bd_get_and_print_battery_usage_from_dump(int fd, long int duration)
                _DBG("%s", dump_p->str);
                if (write(fd, dump_p->str, dump_p->len) < 0)
                        _WARN("write error");
-               g_free(dump_p->str);
+               g_string_free(dump_p, TRUE);
        }
        // Resource usage;
        const char *rid_str = NULL;
@@ -238,7 +245,7 @@ static int bd_get_and_print_battery_usage_from_dump(int fd, long int duration)
                _DBG("%s", dump_p->str);
                if (write(fd, dump_p->str, dump_p->len) < 0)
                        _WARN("write error");
-               g_free(dump_p->str);
+               g_string_free(dump_p, TRUE);
        }
 
        EXIT;
@@ -274,7 +281,7 @@ static int bd_get_and_print_gbr_stat_entry_from_dump(int fd, long int duration)
        _DBG("%s", dump_p->str);//write
        if (write(fd, dump_p->str, dump_p->len) < 0)
                _WARN("write error");
-       g_free(dump_p->str);
+       g_string_free(dump_p, TRUE);
        BM_FREE(gbr_stat);
 
        EXIT;
@@ -284,11 +291,13 @@ static int bd_get_and_print_gbr_stat_entry_from_dump(int fd, long int duration)
 static int bd_get_and_print_gn_stat_entry_from_dump(int fd, long int duration)
 {
        ENTER;
+
+       int ret = BATTERY_MONITOR_ERROR_NONE;
+
        // Dump Network Data
        bd_gl_ntw_stat_s *gn_stat = (bd_gl_ntw_stat_s *)calloc(1, sizeof(bd_gl_ntw_stat_s));
        BM_CHECK_MEM_ALLOC(gn_stat, {});
 
-       int ret = BATTERY_MONITOR_ERROR_NONE;
        ret = bm_server_battery_dump_query_from_db(gn_stat, 2, duration);
        if (ret != BATTERY_MONITOR_ERROR_NONE) {
                _ERR("Unable to fetch data from DB");
@@ -298,6 +307,7 @@ static int bd_get_and_print_gn_stat_entry_from_dump(int fd, long int duration)
 
        GString *dump_p = g_string_sized_new(30);
        BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gn_stat); });
+
        g_string_append(dump_p, TIZEN_DUMP_VERSION);
        g_string_append(dump_p, ",0,l,");
        g_string_append(dump_p, "gn,");
@@ -312,10 +322,13 @@ static int bd_get_and_print_gn_stat_entry_from_dump(int fd, long int duration)
        g_string_append_printf(dump_p, "%d,", 0); //add this to struct as well
        g_string_append_printf(dump_p, "%d", 0);
        g_string_append(dump_p, "\n");
+
        _DBG("%s", dump_p->str);//write
+
        if (write(fd, dump_p->str, dump_p->len) < 0)
                _WARN("write error");
-       g_free(dump_p->str);
+
+       g_string_free(dump_p, TRUE);
        BM_FREE(gn_stat);
 
        EXIT;
@@ -350,7 +363,7 @@ static int bd_get_and_print_gwf_stat_entry_from_dump(int fd, long int duration)
        _DBG("%s", dump_p->str);//write
        if (write(fd, dump_p->str, dump_p->len) < 0)
                _WARN("write error");
-       g_free(dump_p->str);
+       g_string_free(dump_p, TRUE);
 
        dump_p = g_string_sized_new(30);
        BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gwfl_stat); });
@@ -366,7 +379,7 @@ static int bd_get_and_print_gwf_stat_entry_from_dump(int fd, long int duration)
        _DBG("%s", dump_p->str);//write
        if (write(fd, dump_p->str, dump_p->len) < 0)
                _WARN("write error");
-       g_free(dump_p->str);
+       g_string_free(dump_p, TRUE);
        BM_FREE(gwfl_stat);
 
        EXIT;
@@ -402,7 +415,7 @@ static int bd_get_and_print_gble_stat_entry_from_dump(int fd, long int duration)
        _DBG("%s", dump_p->str);//write
        if (write(fd, dump_p->str, dump_p->len) < 0)
                _WARN("write error");
-       g_free(dump_p->str);
+       g_string_free(dump_p, TRUE);
        BM_FREE(gble_stat);
 
        EXIT;
@@ -412,10 +425,12 @@ static int bd_get_and_print_gble_stat_entry_from_dump(int fd, long int duration)
 static int bd_get_and_print_gst_stat_entry_from_dump(int fd, long int duration)
 {
        ENTER;
+
+       int ret = BATTERY_MONITOR_ERROR_NONE;
+
        bd_gl_sgt_stat_s *gst_stat = (bd_gl_sgt_stat_s *)calloc(1, sizeof(bd_gl_sgt_stat_s));
        BM_CHECK_MEM_ALLOC(gst_stat, {});
 
-       int ret = BATTERY_MONITOR_ERROR_NONE;
        ret = bm_server_battery_dump_query_from_db(gst_stat, 4, duration);
        if (ret != BATTERY_MONITOR_ERROR_NONE) {
                _ERR("Unable to fetch data from DB");
@@ -425,6 +440,7 @@ static int bd_get_and_print_gst_stat_entry_from_dump(int fd, long int duration)
 
        GString *dump_p = g_string_sized_new(30);
        BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gst_stat); });
+
        g_string_append(dump_p, TIZEN_DUMP_VERSION);
        g_string_append(dump_p, ",0,l,");
        g_string_append(dump_p, "sgt,");
@@ -436,12 +452,14 @@ static int bd_get_and_print_gst_stat_entry_from_dump(int fd, long int duration)
        g_string_append(dump_p, "\n");
 
        _DBG("%s", dump_p->str);//write
+
        if (write(fd, dump_p->str, dump_p->len) < 0)
                _WARN("write error");
-       g_free(dump_p->str);
+       g_string_free(dump_p, TRUE);
 
        dump_p = g_string_sized_new(30);
        BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gst_stat); });
+
        g_string_append(dump_p, TIZEN_DUMP_VERSION);
        g_string_append(dump_p, ",0,l,");
        g_string_append(dump_p, "sst,");
@@ -452,7 +470,7 @@ static int bd_get_and_print_gst_stat_entry_from_dump(int fd, long int duration)
        if (write(fd, dump_p->str, dump_p->len) < 0)
                _WARN("write error");
 
-       g_free(dump_p->str);
+       g_string_free(dump_p, TRUE);
        BM_FREE(gst_stat);
 
        EXIT;
@@ -488,7 +506,7 @@ static int bd_get_and_print_gwst_stat_entry_from_dump(int fd, long int duration)
        _DBG("%s", dump_p->str);//write
        if (write(fd, dump_p->str, dump_p->len) < 0)
                _WARN("write error");
-       g_free(dump_p->str);
+       g_string_free(dump_p, TRUE);
        BM_FREE(gwst_stat);
 
        EXIT;
@@ -532,7 +550,7 @@ static int bd_get_and_print_gcpu_stat_entry_from_dump(int fd, long int duration)
        _DBG("%s", dump_p->str);
        if (write(fd, dump_p->str, dump_p->len) < 0)
                _WARN("write error");
-       g_free(dump_p->str);
+       g_string_free(dump_p, TRUE);
 
        EXIT;
        return ret;
@@ -556,7 +574,7 @@ static int bd_get_and_print_uid_info_entry_from_dump(int fd)
        g_string_append(dump_p, ",0,i,uid,1000,tizen\n");
        if (write(fd, dump_p->str, dump_p->len) < 0)
                _WARN("write error");
-       g_free(dump_p->str);
+       g_string_free(dump_p, TRUE);
 
        for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
                dump_p = g_string_sized_new(30);
@@ -577,7 +595,7 @@ static int bd_get_and_print_uid_info_entry_from_dump(int fd)
                *temp_id = (amap_data->id + 10000);
                _DBG("Insert id %d, str %s, res %d", *temp_id, temp_str,
                g_hash_table_insert(app_map, temp_id, temp_str));
-               g_free(dump_p->str);
+               g_string_free(dump_p, TRUE);
        }
        bm_set_free_gslist_appid_map(list);
 
@@ -1075,7 +1093,7 @@ int bd_print_history_item_main(int fd, int num_h_items, long long base_time, boo
                _DBG("%s", dump_p->str);
                if (write(fd, dump_p->str, dump_p->len) < 0)
                        _WARN("write error");
-               g_free(dump_p->str);
+               g_string_free(dump_p, TRUE);
        }
 
        // Dump the elapsed time from last event
index 933dc23..d4c0241 100644 (file)
@@ -1768,17 +1768,14 @@ static int bm_battery_stat_query_from_db(sqlite3 *bm_db_handle, void* str_data,
        error_code = BATTERY_MONITOR_ERROR_NONE;
 CATCH:
        if (hstmt != NULL) {
-       rc = bm_query_finalize(hstmt);
+               rc = bm_query_finalize(hstmt);
                if (rc != BATTERY_MONITOR_ERROR_NONE) {
                        error_code = rc;
                        _ERR("finalize error");
                }
-       hstmt = NULL;
-       }
-       if ((error_code != BATTERY_MONITOR_ERROR_NONE) && str_data) {
-               BM_FREE(str_data);
-               str_data = NULL;
+               hstmt = NULL;
        }
+
        if ((error_code == BATTERY_MONITOR_ERROR_NONE) && str_data != NULL) {
                _INFO("Returning Battery Stat");
                return error_code;
@@ -2489,8 +2486,12 @@ int bm_server_battery_dump_query_from_db(void *str_data, int type, long int dura
        ENTER;
        int ret = BATTERY_MONITOR_ERROR_NONE;
 
-       BM_RETURN_VAL((g_hBatteryMonitorDB != NULL), { ret = BATTERY_MONITOR_ERROR_DB_NOT_OPENED; }, ret, ("The database isn't connected."));
-       BM_RETURN_VAL((str_data != NULL), { ret  = BATTERY_MONITOR_ERROR_INVALID_PARAMETER; }, ret, ("The Battery Dump Handle is NULL"));
+       BM_RETURN_VAL((g_hBatteryMonitorDB != NULL), \
+               { ret = BATTERY_MONITOR_ERROR_DB_NOT_OPENED; }, ret, ("The database isn't connected."));
+
+       BM_RETURN_VAL((str_data != NULL), \
+               { ret  = BATTERY_MONITOR_ERROR_INVALID_PARAMETER; }, ret, ("The Battery Dump Handle is NULL"));
+
        ret = bm_battery_stat_query_from_db(g_hBatteryMonitorDB, str_data, type, duration);
 
        EXIT;