From 46a76434f6f1f5f1c7c21fb5bccd0577348613cd Mon Sep 17 00:00:00 2001 From: sungrae jo Date: Wed, 6 Nov 2019 18:17:25 +0900 Subject: [PATCH] Added valid check about command handle code. Change-Id: Ic8259dd16160ee460bbec6f03f22e97c6085423d Signed-off-by: sungrae jo --- client/vc.c | 6 ++- client/vc_mgr.c | 18 +++++--- client/vc_widget.c | 1 + common/vc_cmd_db.c | 55 +++++++++++++++++++++++-- common/vc_command.c | 115 ++++++++++++++++++++++++++++++++++++++++++++++++---- server/vcd_server.c | 18 ++++++++ 6 files changed, 196 insertions(+), 17 deletions(-) diff --git a/client/vc.c b/client/vc.c index f63e842..ebd15ba 100644 --- a/client/vc.c +++ b/client/vc.c @@ -1763,9 +1763,12 @@ static Eina_Bool __vc_notify_result(void *data) SLOG(LOG_INFO, TAG_VCC, "Client result callback called"); vc_cmd_list_destroy(vc_cmd_list, true); + vc_cmd_list = NULL; /* Release result */ - if (NULL != temp_text) free(temp_text); + if (temp_text) + free(temp_text); + temp_text = NULL; return EINA_FALSE; } @@ -1832,6 +1835,7 @@ int vc_get_result(vc_result_cb callback, void* user_data) vc_client_not_use_callback(g_vc); vc_cmd_list_destroy(vc_cmd_list, true); + vc_cmd_list = NULL; /* Release result */ if (NULL != temp_text) { diff --git a/client/vc_mgr.c b/client/vc_mgr.c index cee3254..921f04f 100644 --- a/client/vc_mgr.c +++ b/client/vc_mgr.c @@ -2529,7 +2529,7 @@ static void __vc_mgr_notify_all_result(vc_result_type_e result_type) vc_cmd_print_list(vc_cmd_list); - bool cb_ret; + bool cb_ret = false; vc_mgr_client_set_all_result(g_vc_m, event, temp_text); @@ -2564,7 +2564,9 @@ static void __vc_mgr_notify_all_result(vc_result_type_e result_type) } /* Release list */ - vc_cmd_list_destroy(vc_cmd_list, true); + if (vc_cmd_list) + vc_cmd_list_destroy(vc_cmd_list, true); + vc_cmd_list = NULL; vc_mgr_client_set_exclusive_command(g_vc_m, false); @@ -2601,7 +2603,9 @@ static void __vc_mgr_notify_all_result(vc_result_type_e result_type) } /* Release list */ - vc_cmd_list_destroy(vc_cmd_list, true); + if (vc_cmd_list) + vc_cmd_list_destroy(vc_cmd_list, true); + vc_cmd_list = NULL; return; } @@ -2637,10 +2641,14 @@ static Eina_Bool __vc_mgr_notify_result(void *data) vc_mgr_client_not_use_callback(g_vc_m); SLOG(LOG_INFO, TAG_VCM, "Result callback called"); - vc_cmd_list_destroy(vc_cmd_list, true); + if (vc_cmd_list) + vc_cmd_list_destroy(vc_cmd_list, true); + vc_cmd_list = NULL; /* Release result */ - if (NULL != temp_text) free(temp_text); + if (temp_text) + free(temp_text); + temp_text = NULL; return EINA_FALSE; } diff --git a/client/vc_widget.c b/client/vc_widget.c index 950f7fc..89faa97 100644 --- a/client/vc_widget.c +++ b/client/vc_widget.c @@ -1412,6 +1412,7 @@ static Eina_Bool __vc_widget_notify_result(void *data) if (NULL != temp_text) free(temp_text); vc_cmd_list_destroy(vc_cmd_list, true); + vc_cmd_list = NULL; return EINA_FALSE; } diff --git a/common/vc_cmd_db.c b/common/vc_cmd_db.c index f3889d5..9fdb4ec 100644 --- a/common/vc_cmd_db.c +++ b/common/vc_cmd_db.c @@ -1028,6 +1028,7 @@ static int __vc_db_get_result(sqlite3* db_handle, char** result_text, int* event SLOG(LOG_DEBUG, vc_db_tag(), "Fail to add command to list"); vc_cmd_destroy(temp_cmd); vc_cmd_list_destroy(vc_cmd_list, true); + vc_cmd_list = NULL; if (NULL != *result_text) { free(*result_text); *result_text = NULL; @@ -1345,6 +1346,7 @@ static int __vc_db_append_commands(sqlite3* db_handle, int pid, int type, vc_cmd SLOG(LOG_DEBUG, vc_db_tag(), "Fail to add command to list"); vc_cmd_destroy(temp_cmd); vc_cmd_list_destroy(vc_cmd_list, true); + vc_cmd_list = NULL; sqlite3_reset(stmt); sqlite3_clear_bindings(stmt); sqlite3_finalize(stmt); @@ -1416,6 +1418,51 @@ static vc_cmd_s* __vc_db_command_copy(vc_cmd_s* src_cmd) return temp_cmd; } +static int __vc_db_command_destroy(vc_cmd_h vc_command) +{ + if (NULL == vc_command) { + SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Input parameter is NULL"); + return VC_ERROR_INVALID_PARAMETER; + } + + vc_cmd_s* command = NULL; + command = (vc_cmd_s*)vc_command; + + SLOG(LOG_DEBUG, vc_db_tag(), "[Destroy command][%p]", command); + + if (NULL != command) { + if (NULL != command->command) { + free(command->command); + command->command = NULL; + } + if (NULL != command->parameter) { + free(command->parameter); + command->parameter = NULL; + } + if (NULL != command->invocation_name) { + free(command->invocation_name); + command->invocation_name = NULL; + } + if (NULL != command->appid) { + free(command->appid); + command->appid = NULL; + } + if (NULL != command->fixed) { + free(command->fixed); + command->fixed = NULL; + } + if (NULL != command->coordinates) { + free(command->coordinates); + command->coordinates = NULL; + } + free(command); + command = NULL; + } + + return VC_ERROR_NONE; +} + + static int __vc_db_create_table(sqlite3* db_handle, const char* table) { SLOG(LOG_INFO, vc_db_tag(), "[INFO] Create DB table (%s)", table); @@ -2114,7 +2161,7 @@ static int __vc_db_insert_command(sqlite3* db_handle, int pid, vc_cmd_type_e typ SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to generate command, %d", ret); if (NULL != tmp_cmd) { - vc_cmd_destroy((vc_cmd_h)tmp_cmd); + __vc_db_command_destory((vc_cmd_h)tmp_cmd); } return ret; } @@ -2144,7 +2191,7 @@ static int __vc_db_insert_command(sqlite3* db_handle, int pid, vc_cmd_type_e typ free(fixed_cmd); fixed_cmd = NULL; } - vc_cmd_destroy((vc_cmd_h)tmp_cmd); + __vc_db_command_destory((vc_cmd_h)tmp_cmd); break; } @@ -2162,7 +2209,7 @@ static int __vc_db_insert_command(sqlite3* db_handle, int pid, vc_cmd_type_e typ free(fixed_cmd); fixed_cmd = NULL; } - vc_cmd_destroy((vc_cmd_h)tmp_cmd); + __vc_db_command_destory((vc_cmd_h)tmp_cmd); break; } } @@ -2198,7 +2245,7 @@ static int __vc_db_insert_command(sqlite3* db_handle, int pid, vc_cmd_type_e typ free(fixed_cmd); fixed_cmd = NULL; } - vc_cmd_destroy((vc_cmd_h)tmp_cmd); + __vc_db_command_destory((vc_cmd_h)tmp_cmd); return VC_DB_ERROR_NONE; } diff --git a/common/vc_command.c b/common/vc_command.c index 08f5c9f..d1ca79b 100644 --- a/common/vc_command.c +++ b/common/vc_command.c @@ -37,6 +37,7 @@ static int g_feature_enabled = -1; static int g_privilege_allowed = 1; /* Always True */ static cynara *p_cynara = NULL; +static GList *g_cmd_list = NULL; static GList *g_cmdlist_list = NULL; @@ -154,6 +155,74 @@ static int __vc_cmd_check_privilege() return VC_ERROR_NONE; } +static bool __vc_cmd_is_valid(vc_cmd_h vc_command) +{ + if (NULL == g_cmd_list) { + SLOG(LOG_WARN, TAG_VCCMD, "[WARNING] cmd_list is not valid"); + return false; + } + + if (g_list_length(g_cmd_list) == 0) { + SLOG(LOG_WARN, TAG_VCCMD, "[WARNING] cmd_list is not valid"); + return false; + } + + bool flag = false; + vc_cmd_s *command = NULL; + command = (vc_cmd_s *)vc_command; + + GList *iter = NULL; + iter = g_list_first(g_cmd_list); + while (NULL != iter) { + vc_cmd_s *data = NULL; + data = iter->data; + if (NULL != data && command == data) { + flag = true; + break; + } + iter = g_list_next(iter); + } + + if (false == flag) + SLOG(LOG_WARN, TAG_VCCMD, "[WARNING] command(%p) is not valid", command); + + return flag; +} + +static bool __vc_cmd_list_is_valid(vc_cmd_list_h vc_cmd_list) +{ + if (NULL == g_cmdlist_list) { + SLOG(LOG_WARN, TAG_VCCMD, "[WARNING] cmdlist_list is not valid"); + return false; + } + + if (g_list_length(g_cmdlist_list) == 0) { + SLOG(LOG_WARN, TAG_VCCMD, "[WARNING] cmdlist_list is not valid"); + return false; + } + + bool flag = false; + vc_cmd_list_s *list = NULL; + list = (vc_cmd_list_s *)vc_cmd_list; + + GList *iter = NULL; + iter = g_list_first(g_cmdlist_list); + while (NULL != iter) { + vc_cmd_list_s *data = NULL; + data = iter->data; + if (NULL != data && list == data) { + flag = true; + break; + } + iter = g_list_next(iter); + } + + if (false == flag) + SLOG(LOG_WARN, TAG_VCCMD, "[WARNING] list(%p) is not valid", list); + + return flag; +} + int vc_cmd_list_create(vc_cmd_list_h* vc_cmd_list) { if (0 != __vc_cmd_get_feature_enabled()) { @@ -201,6 +270,11 @@ int vc_cmd_list_destroy(vc_cmd_list_h vc_cmd_list, bool release_command) return VC_ERROR_INVALID_PARAMETER; } + if (false == __vc_cmd_list_is_valid(vc_cmd_list)) { + SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Input parameter is NULL"); + return VC_ERROR_INVALID_PARAMETER; + } + vc_cmd_list_remove_all(vc_cmd_list, release_command); vc_cmd_list_s* list = NULL; @@ -218,7 +292,7 @@ int vc_cmd_list_destroy(vc_cmd_list_h vc_cmd_list, bool release_command) while (NULL != iter) { data = iter->data; - if (data && list == data) { + if (NULL != data && list == data) { g_cmdlist_list = g_list_remove_link(g_cmdlist_list, iter); free(data); @@ -226,6 +300,7 @@ int vc_cmd_list_destroy(vc_cmd_list_h vc_cmd_list, bool release_command) SLOG(LOG_DEBUG, TAG_VCCMD, "Client destroy"); g_list_free(iter); + iter = NULL; return VC_ERROR_NONE; } @@ -373,12 +448,8 @@ int vc_cmd_list_remove_all(vc_cmd_list_h vc_cmd_list, bool release_command) , list, release_command ? "true" : "false"); int count = g_slist_length(list->list); - - int i ; - vc_cmd_s *temp_cmd; - - for (i = 0; i < count ; i++) { - temp_cmd = g_slist_nth_data(list->list, 0); + for (int i = 0; i < count ; i++) { + vc_cmd_s *temp_cmd = g_slist_nth_data(list->list, 0); if (NULL != temp_cmd) { list->list = g_slist_remove(list->list, temp_cmd); @@ -733,6 +804,8 @@ int vc_cmd_create(vc_cmd_h* vc_command) *vc_command = (vc_cmd_h)command; + g_cmd_list = g_list_append(g_cmd_list, command); + SLOG(LOG_DEBUG, TAG_VCCMD, "[Create command][%p]", *vc_command); return VC_ERROR_NONE; @@ -752,11 +825,39 @@ int vc_cmd_destroy(vc_cmd_h vc_command) return VC_ERROR_INVALID_PARAMETER; } + if (false == __vc_cmd_is_valid(vc_command)) { + SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Input parameter is NULL"); + return VC_ERROR_INVALID_PARAMETER; + } + vc_cmd_s* command = NULL; command = (vc_cmd_s*)vc_command; SLOG(LOG_DEBUG, TAG_VCCMD, "[Destroy command][%p]", command); + bool flag = false; + GList *iter = NULL; + iter = g_list_first(g_cmd_list); + while (NULL != iter) { + vc_cmd_s *data = NULL; + data = iter->data; + if (NULL != data && command == data) { + g_cmd_list = g_list_remove_link(g_cmd_list, iter); + flag = true; + break; + } + iter = g_list_next(iter); + } + + if (false == flag) { + SLOG(LOG_ERROR, TAG_VCCMD, "Fail to destroy client : handle is not valid"); + return VC_ERROR_INVALID_PARAMETER; + } + + if (iter) + g_list_free(iter); + iter = NULL; + if (NULL != command) { if (NULL != command->command) { free(command->command); diff --git a/server/vcd_server.c b/server/vcd_server.c index c4425d0..8f8c608 100644 --- a/server/vcd_server.c +++ b/server/vcd_server.c @@ -526,6 +526,7 @@ int vcd_send_result(vce_result_event_e event, int* result_id, int count, const c if (!filtered_id) { SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to allocate memory"); vc_cmd_list_destroy(vc_cmd_list, true); + vc_cmd_list = NULL; return VCD_ERROR_OUT_OF_MEMORY; } int filtered_count = 0; @@ -593,6 +594,7 @@ int vcd_send_result(vce_result_event_e event, int* result_id, int count, const c vcd_config_set_service_state(VCD_STATE_READY); vcdc_send_service_state(VCD_STATE_READY); vc_cmd_list_destroy(vc_cmd_list, true); + vc_cmd_list = NULL; if (NULL != filtered_id) { free(filtered_id); filtered_id = NULL; @@ -691,6 +693,7 @@ int vcd_send_result(vce_result_event_e event, int* result_id, int count, const c if (0 != vc_cmd_list_create(&widget_cmd_list)) { SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to create widget command list handle"); vc_cmd_list_destroy(vc_cmd_list, true); + vc_cmd_list = NULL; return VCD_ERROR_OUT_OF_MEMORY; } @@ -709,7 +712,9 @@ int vcd_send_result(vce_result_event_e event, int* result_id, int count, const c if (0 != vc_cmd_list_create(&foreground_cmd_list)) { SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to create foreground command list handle"); vc_cmd_list_destroy(vc_cmd_list, true); + vc_cmd_list = NULL; vc_cmd_list_destroy(widget_cmd_list, true); + widget_cmd_list = NULL; return VCD_ERROR_OUT_OF_MEMORY; } @@ -726,8 +731,10 @@ int vcd_send_result(vce_result_event_e event, int* result_id, int count, const c } } vc_cmd_list_destroy(foreground_cmd_list, true); + foreground_cmd_list = NULL; } vc_cmd_list_destroy(widget_cmd_list, true); + widget_cmd_list = NULL; } else { SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is NULL"); } @@ -757,6 +764,7 @@ int vcd_send_result(vce_result_event_e event, int* result_id, int count, const c vcd_client_manager_set_exclusive(false); vc_cmd_list_destroy(vc_cmd_list, true); + vc_cmd_list = NULL; return VCD_ERROR_NONE; } @@ -778,13 +786,16 @@ int vcd_send_result(vce_result_event_e event, int* result_id, int count, const c if (0 != vc_cmd_list_create(&temp_list)) { SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to create widget command list handle"); vc_cmd_list_destroy(vc_cmd_list, true); + vc_cmd_list = NULL; return VCD_ERROR_OUT_OF_MEMORY; } if (0 != vc_cmd_list_create(&widget_cmd_list)) { SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to create widget command list handle"); vc_cmd_list_destroy(vc_cmd_list, true); + vc_cmd_list = NULL; vc_cmd_list_destroy(temp_list, true); + temp_list = NULL; return VCD_ERROR_OUT_OF_MEMORY; } @@ -808,7 +819,9 @@ int vcd_send_result(vce_result_event_e event, int* result_id, int count, const c if (0 != vc_cmd_list_create(&foreground_cmd_list)) { SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to create foreground command list handle"); vc_cmd_list_destroy(vc_cmd_list, true); + vc_cmd_list = NULL; vc_cmd_list_destroy(widget_cmd_list, true); + widget_cmd_list = NULL; return VCD_ERROR_OUT_OF_MEMORY; } @@ -830,8 +843,10 @@ int vcd_send_result(vce_result_event_e event, int* result_id, int count, const c } } vc_cmd_list_destroy(foreground_cmd_list, true); + foreground_cmd_list = NULL; } vc_cmd_list_destroy(widget_cmd_list, true); + widget_cmd_list = NULL; } int pid = vcd_client_widget_get_foreground_pid(); @@ -842,6 +857,7 @@ int vcd_send_result(vce_result_event_e event, int* result_id, int count, const c vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false); vc_cmd_list_destroy(vc_cmd_list, true); + vc_cmd_list = NULL; if (-1 != vcd_client_manager_get_pid()) { /* Manager client is available */ @@ -856,6 +872,7 @@ int vcd_send_result(vce_result_event_e event, int* result_id, int count, const c /* exclusive command */ vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true); vc_cmd_list_destroy(vc_cmd_list, true); + vc_cmd_list = NULL; if (-1 != vcd_client_manager_get_pid()) { /* Manager client is available */ @@ -1047,6 +1064,7 @@ int vcd_send_result(vce_result_event_e event, int* result_id, int count, const c } vc_cmd_list_destroy(vc_cmd_list, true); + vc_cmd_list = NULL; return; #endif -- 2.7.4