Prevent memory leak in fail case 72/236072/4
authorDewal Agarwal <d1.agarwal@samsung.com>
Fri, 12 Jun 2020 11:39:05 +0000 (17:09 +0530)
committerDewal Agarwal <d1.agarwal@samsung.com>
Mon, 15 Jun 2020 09:42:15 +0000 (09:42 +0000)
Change-Id: If28ad0e5e976cf57376bfe31cedce561867bc76b
Signed-off-by: Dewal Agarwal <d1.agarwal@samsung.com>
src/bm_server_db.c

index db079f1..380df48 100644 (file)
@@ -1137,6 +1137,10 @@ static int bm_battery_stat_insert_to_db(sqlite3 *bm_db_handle, void *str_data, i
 {
        ENTER;
 
+       if(str_data == NULL) {
+               _ERR("Invalid Stat");
+               return BATTERY_MONITOR_ERROR_INVALID_PARAMETER;
+       }
        int rc = 0;
        char query[BATTERY_MONITOR_SQL_LEN_MAX] = {0, };
        int error_code = BATTERY_MONITOR_ERROR_NONE;
@@ -1291,7 +1295,8 @@ static int bm_battery_stat_insert_to_db(sqlite3 *bm_db_handle, void *str_data, i
                bm_gl_cpu_stat_convert_to_sql(gcpu_st, hstmt, query);
                break;
        default:
-               break;
+               _ERR("Invalid Type");
+               return BATTERY_MONITOR_ERROR_INVALID_PARAMETER;
        }
 
        rc = bm_query_step(hstmt);
@@ -1304,8 +1309,8 @@ static int bm_battery_stat_insert_to_db(sqlite3 *bm_db_handle, void *str_data, i
        }
 
        rc = bm_query_finalize(hstmt);
-       BM_RETURN_VAL((rc == BATTERY_MONITOR_ERROR_NONE), {}, rc, ("finalize error"));
        hstmt = NULL;
+       BM_RETURN_VAL((rc == BATTERY_MONITOR_ERROR_NONE), {}, rc, ("finalize error"));
 
        EXIT;
        return error_code;
@@ -2234,30 +2239,26 @@ static int bm_delete_table_by_time_interval(sqlite3 *bm_db_handle, const char* t
                BM_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE time_s <= %ld", table, duration);
 
        hstmt = bm_prepare_query(bm_db_handle, query);
-       if (hstmt == NULL) {
-               _ERR("bm_svc_query_prepare(%s) failed(%s).\n", query, bm_db_err_msg(bm_db_handle));
-               error_code = BATTERY_MONITOR_ERROR_DB_FAILED;
-               goto END;
+       if (bm_db_err_code(bm_db_handle) != SQLITE_OK || hstmt == NULL) {
+               _ERR("Access failed(%s)", bm_db_err_msg(bm_db_handle));
+               return BATTERY_MONITOR_ERROR_DB_FAILED;
        }
 
        rc = bm_query_step(hstmt);
        if (rc != SQLITE_DONE) {
-               _ERR("The record isn't found\n");
-               error_code = BATTERY_MONITOR_ERROR_RECORD_NOT_FOUND;
-               goto END;
+               _ERR("Query Failed");
+               error_code = BATTERY_MONITOR_ERROR_DB_FAILED;
        }
 
        rc = bm_query_finalize(hstmt);
        if (rc != BATTERY_MONITOR_ERROR_NONE) {
                _ERR("finalize error");
-               error_code = rc;
-               goto END;
+               error_code = BATTERY_MONITOR_ERROR_DB_FAILED;
        }
 
        hstmt =  NULL;
-       EXIT;
 
-END:
+       EXIT;
        return error_code;
 }
 
@@ -2634,10 +2635,10 @@ int bm_server_delete_table_by_time_interval(void)
                pthread_mutex_unlock(&battery_monitor_mutex);
                return error_code;
        }
-       error_code = bm_delete_table_by_time_interval(g_hBatteryMonitorDB, BATTERY_MONITOR_GBL_NT_STAT_TABLE);
+       error_code = bm_delete_table_by_time_interval(g_hBatteryMonitorDB, BATTERY_MONITOR_GBL_CPU_STAT_TABLE);
        if (error_code != BATTERY_MONITOR_ERROR_NONE) {
                ret_transaction = bm_end_transaction(g_hBatteryMonitorDB, false);
-               _ERR("Delete nt_stat fail, rollback delete query(%d)!!!!\n", ret_transaction);
+               _ERR("Delete gcpu_stat fail, rollback delete query(%d)!!!!\n", ret_transaction);
                pthread_mutex_unlock(&battery_monitor_mutex);
                return error_code;
        }
@@ -2669,6 +2670,13 @@ int bm_server_delete_table_by_time_interval(void)
                pthread_mutex_unlock(&battery_monitor_mutex);
                return error_code;
        }
+       error_code = bm_delete_table_by_time_interval(g_hBatteryMonitorDB, BATTERY_MONITOR_GBL_NT_STAT_TABLE);
+       if (error_code != BATTERY_MONITOR_ERROR_NONE) {
+               ret_transaction = bm_end_transaction(g_hBatteryMonitorDB, false);
+               _ERR("Delete nt_stat fail, rollback delete query(%d)!!!!\n", ret_transaction);
+               pthread_mutex_unlock(&battery_monitor_mutex);
+               return error_code;
+       }
 
        pthread_mutex_unlock(&battery_monitor_mutex);
        bm_end_transaction(g_hBatteryMonitorDB, true);