Deallocate the list memory after using it 82/283782/1
authorSuyeon Hwang <stom.hwang@samsung.com>
Mon, 31 Oct 2022 06:12:28 +0000 (15:12 +0900)
committerTizen AI <ai.tzn.sec@samsung.com>
Thu, 3 Nov 2022 01:47:52 +0000 (10:47 +0900)
- Issue:
Command list was note deallocated after using it.

- Solution:
vc_cmd_db module creates the command list for update commands DB, but
after using it, the module did not deallocate the list. It can occur
memory leak. Thus, this patch adds code for deallocating the list.

Change-Id: Iffc7acb78aa577fdd56a27efd1abdc2a802d05eb
Signed-off-by: Suyeon Hwang <stom.hwang@samsung.com>
common/vc_cmd_db.c

index 9d572d0..e41b291 100644 (file)
@@ -2488,6 +2488,15 @@ int vc_db_delete_commands(int pid, vc_cmd_type_e type, char* appid)
        return VC_DB_ERROR_NONE;
 }
 
+static void __free_command_list_element(gpointer data)
+{
+       vc_cmd_h temp_cmd = (vc_cmd_h)data;
+       int ret = vc_cmd_destroy(temp_cmd);
+       if (ret != VC_ERROR_NONE) {
+               SLOG(LOG_ERROR, vc_db_tag(), "Fail to destroy command. (%d/%s)", ret, get_error_message(ret));
+       }
+}
+
 int vc_db_backup_command()
 {
        GSList* list = NULL;
@@ -2515,13 +2524,13 @@ int vc_db_backup_command()
        if (ret != VC_DB_ERROR_NONE) {
                SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to insert command list to backup db");
                __vc_db_rollback_transaction(g_db_backup_handle);
-               return ret;
+       } else {
+               SLOG(LOG_ERROR, vc_db_tag(), "[SUCCESS] Backup commands");
+               __vc_db_commit_transaction(g_db_backup_handle);
        }
 
-       __vc_db_commit_transaction(g_db_backup_handle);
-
-       SLOG(LOG_ERROR, vc_db_tag(), "[SUCCESS] Backup commands");
-       return VC_DB_ERROR_NONE;
+       g_slist_free_full(list, __free_command_list_element);
+       return ret;
 }
 
 int vc_db_restore_command()
@@ -2541,10 +2550,11 @@ int vc_db_restore_command()
 
        ret = vc_db_insert_commands_list(-1, VC_COMMAND_TYPE_BACKGROUND, list, NULL, true);
        if (ret != VC_DB_ERROR_NONE) {
-               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to insert command list");
-               return ret;
+               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to insert command list. (%d)", ret);
+       } else {
+               SLOG(LOG_ERROR, vc_db_tag(), "[SUCCESS] Restore commands");
        }
 
-       SLOG(LOG_ERROR, vc_db_tag(), "[SUCCESS] Restore commands");
-       return VC_DB_ERROR_NONE;
+       g_slist_free_full(list, __free_command_list_element);
+       return ret;
 }