From 2a9554d0f6ba6bb12debc1ee4fcb4eb0fa8936f1 Mon Sep 17 00:00:00 2001 From: Suyeon Hwang Date: Mon, 27 Feb 2017 15:41:42 +0900 Subject: [PATCH] Fix memory leak / Add type casting Change-Id: I4662a6c54b34717ef0749b8beea9a4d3e2e8d09a Signed-off-by: Suyeon Hwang (cherry picked from commit ba53ae91923a97f37f3c0020d9e192415fff8430) --- common/vc_cmd_db.c | 25 ++++++++++++++++++++++++- common/vc_command.c | 37 +++++++++++++++++++++++++++++++------ common/vc_config_parser.c | 2 ++ common/vc_info_parser.c | 16 +++++++++++++++- include/voice_control.h | 2 +- server/vcd_engine_agent.c | 23 +++++++++++++++++++---- 6 files changed, 92 insertions(+), 13 deletions(-) diff --git a/common/vc_cmd_db.c b/common/vc_cmd_db.c index 3cf4094..4218894 100644 --- a/common/vc_cmd_db.c +++ b/common/vc_cmd_db.c @@ -897,6 +897,20 @@ static int __vc_db_get_result(char** result_text, int* event, char** msg, int pi return VC_DB_ERROR_NONE; } +void __vc_db_demandable_client_free(void* data) +{ + vc_deactivated_app_s* d_app = (vc_deactivated_app_s*)data; + + if (NULL != d_app) { + if (NULL != d_app->appid) { + free(d_app->appid); + d_app->appid = NULL; + } + + free(d_app); + } +} + static int __vc_db_get_appid(const char* result, GSList** app_list) { GSList* temp_app_list = NULL; @@ -940,7 +954,7 @@ static int __vc_db_get_appid(const char* result, GSList** app_list) SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] memory allcation fail"); if (NULL != temp_app_list) { - g_slist_free_full(temp_app_list, free); + g_slist_free_full(temp_app_list, __vc_db_demandable_client_free); temp_app_list = NULL; } @@ -1225,6 +1239,9 @@ int vc_db_initialize(void) db_util_close(db_handle); db_handle = NULL; } + + free(path); + path = NULL; return VC_DB_ERROR_OPERATION_FAILED; } @@ -1234,6 +1251,9 @@ int vc_db_initialize(void) if (db_handle) db_util_close(db_handle); db_handle = NULL; + + free(path); + path = NULL; return VC_DB_ERROR_OPERATION_FAILED; } @@ -1242,6 +1262,9 @@ int vc_db_initialize(void) if (db_handle) db_util_close(db_handle); db_handle = NULL; + + free(path); + path = NULL; return VC_DB_ERROR_OPERATION_FAILED; } diff --git a/common/vc_command.c b/common/vc_command.c index 3f3f719..bd0511d 100644 --- a/common/vc_command.c +++ b/common/vc_command.c @@ -1738,7 +1738,12 @@ static int __vc_cmd_trelative_check(const char *str, struct tm *td, int *exist) ret = regexec(®[4], str, 2, pmatch, 0); if (0 == ret) { len = pmatch[1].rm_eo - pmatch[1].rm_so; - tempstr = strndup(str + pmatch[1].rm_so, len); + + if (0 > len) { + SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid string length"); + return VC_ERROR_OPERATION_FAILED; + } + tempstr = strndup(str + pmatch[1].rm_so, (size_t)len); if (NULL == tempstr) { SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Memory allocation is failed"); @@ -1759,7 +1764,12 @@ static int __vc_cmd_trelative_check(const char *str, struct tm *td, int *exist) ret = regexec(®[5], str, 2, pmatch, 0); if (0 == ret) { len = pmatch[1].rm_eo - pmatch[1].rm_so; - tempstr = strndup(str + pmatch[1].rm_so, len); + + if (0 > len) { + SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid string length"); + return VC_ERROR_OPERATION_FAILED; + } + tempstr = strndup(str + pmatch[1].rm_so, (size_t)len); if (NULL == tempstr) { SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Memory allocation is failed"); @@ -1828,7 +1838,12 @@ static int __vc_cmd_tabsolute_check(const char *str, struct tm *td, int *exist) ret = regexec(®[1], str, 2, pmatch, 0); if (0 == ret) { len = pmatch[1].rm_eo - pmatch[1].rm_so; - tempstr = strndup(str + pmatch[1].rm_so, len); + + if (0 > len) { + SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid string length"); + return VC_ERROR_OPERATION_FAILED; + } + tempstr = strndup(str + pmatch[1].rm_so, (size_t)len); if (NULL == tempstr) { SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Memory allocation is failed"); @@ -1866,7 +1881,7 @@ static int __vc_cmd_tabsolute_check(const char *str, struct tm *td, int *exist) idx = 1; len = pmatch[idx].rm_eo - pmatch[idx].rm_so; if (0 < len) { - tempstr = strndup(str + pmatch[idx].rm_so, len); + tempstr = strndup(str + pmatch[idx].rm_so, (size_t)len); if (NULL == tempstr) { SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Memory allocation is failed"); @@ -2015,7 +2030,12 @@ static int __vc_cmd_dabsolute_check(const char *str, struct tm *td, int *exist) ret = regexec(®[9], str, 2, pmatch, 0); if (0 == ret) { len = pmatch[1].rm_eo - pmatch[1].rm_so; - tempstr = strndup(str + pmatch[1].rm_so, len); + + if (0 > len) { + SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid string length"); + return VC_ERROR_OPERATION_FAILED; + } + tempstr = strndup(str + pmatch[1].rm_so, (size_t)len); if (NULL == tempstr) { SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Memory allocation is failed"); @@ -2079,7 +2099,12 @@ static int __vc_cmd_dabsolute_check(const char *str, struct tm *td, int *exist) if (!m_flag) return -1; len = pmatch[2].rm_eo - pmatch[2].rm_so; - tempstr = strndup(str + pmatch[2].rm_so, len); + + if (0 > len) { + SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid string length"); + return VC_ERROR_OPERATION_FAILED; + } + tempstr = strndup(str + pmatch[2].rm_so, (size_t)len); if (NULL == tempstr) { SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Memory allocation is failed"); diff --git a/common/vc_config_parser.c b/common/vc_config_parser.c index 050d697..eb9c867 100644 --- a/common/vc_config_parser.c +++ b/common/vc_config_parser.c @@ -225,6 +225,8 @@ int vc_parser_free_engine_info(vc_engine_info_s* engine_info) } } + g_slist_free(engine_info->languages); + if (NULL != engine_info) free(engine_info); return 0; diff --git a/common/vc_info_parser.c b/common/vc_info_parser.c index 770ca29..566e83b 100644 --- a/common/vc_info_parser.c +++ b/common/vc_info_parser.c @@ -145,6 +145,20 @@ int vc_cmd_parser_append_commands(int pid, vc_cmd_type_e type, vc_cmd_list_h vc_ return ret; } +void __vc_info_parser_demandable_client_free(void* data) +{ + vc_demandable_client_s* d_client = (vc_demandable_client_s*)data; + + if (NULL != d_client) { + if (NULL != d_client->appid) { + free(d_client->appid); + d_client->appid = NULL; + } + + free(d_client); + } +} + int vc_info_parser_get_demandable_clients(GSList** client_list) { /* Check file */ @@ -193,7 +207,7 @@ int vc_info_parser_get_demandable_clients(GSList** client_list) SLOG(LOG_ERROR, vc_info_tag(), "[ERROR] Memory alloc error!!"); if (NULL != temp_client_list) { - g_slist_free_full(temp_client_list, free); + g_slist_free_full(temp_client_list, __vc_info_parser_demandable_client_free); temp_client_list = NULL; } xmlFree(key); diff --git a/include/voice_control.h b/include/voice_control.h index d8ccd26..154dce9 100644 --- a/include/voice_control.h +++ b/include/voice_control.h @@ -231,7 +231,7 @@ int vc_get_state(vc_state_e* state); * @retval #VC_ERROR_INVALID_PARAMETER Invalid parameter * @retval #VC_ERROR_PERMISSION_DENIED Permission denied * @retval #VC_ERROR_NOT_SUPPORTED Not supported - * @pre The state should be #VC_STATE_READY. + * @pre The state should be #VC_STATE_READY. * @see vc_request_start() * @see vc_request_stop() * @see vc_request_cancel() diff --git a/server/vcd_engine_agent.c b/server/vcd_engine_agent.c index 3d9ec29..8166f36 100755 --- a/server/vcd_engine_agent.c +++ b/server/vcd_engine_agent.c @@ -170,10 +170,29 @@ int vcd_engine_agent_release() /* Get handle data from list */ data = iter->data; iter = g_list_remove(iter, data); + + if (NULL != data) { + if (NULL != data->engine_uuid) { + free(data->engine_uuid); + data->engine_uuid = NULL; + } + if (NULL != data->engine_name) { + free(data->engine_name); + data->engine_name = NULL; + } + if (NULL != data->engine_path) { + free(data->engine_path); + data->engine_path = NULL; + } + + free(data); + data = NULL; + } } } g_list_free(iter); + g_engine_list = NULL; /* release current engine data */ if (NULL != g_dynamic_engine.pefuncs) { @@ -1133,7 +1152,3 @@ int __log_enginelist() return 0; } - - - - -- 2.7.4