From 8845d278c240c0cf10cffcdc3ccf177082eb64b4 Mon Sep 17 00:00:00 2001 From: Jihoon Kim Date: Tue, 6 Sep 2022 19:13:57 +0900 Subject: [PATCH] Add invalid parameter check in setting APIs Change-Id: Ibdd6336b50403124d65248d45f41a85ac675629a Signed-off-by: Jihoon Kim --- common/multi_assistant_settings.c | 151 +++++++++++++++++------------ include/multi_assistant_settings.h | 7 +- 2 files changed, 93 insertions(+), 65 deletions(-) diff --git a/common/multi_assistant_settings.c b/common/multi_assistant_settings.c index fb1b1a3..3a9f376 100644 --- a/common/multi_assistant_settings.c +++ b/common/multi_assistant_settings.c @@ -53,6 +53,11 @@ int ma_settings_get_current_voice_assistant(char** app_id) { bool multiple = false; int res = MA_ERROR_NONE; + if (!app_id) { + LOGE("Invalid parameter"); + return MA_ERROR_INVALID_PARAMETER; + } + res = ma_settings_is_multiple_mode(&multiple); LOGI("multiple mode(%d)", multiple); if (MA_ERROR_NONE != res) return res; @@ -75,11 +80,16 @@ int ma_settings_change_voice_assistant(const char* app_id) { bool multiple = false; int res = MA_ERROR_NONE; + if (!app_id) { + LOGE("Invalid parameter"); + return MA_ERROR_INVALID_PARAMETER; + } + res = ma_settings_is_multiple_mode(&multiple); LOGD("multiple mode(%d). Error(%x)", multiple, res); if (MA_ERROR_NONE != res) return res; - if (!multiple && app_id) { + if (!multiple) { if (0 == vconf_set_str(DEFAULT_ASSISTANT_URL, app_id)) { LOGI("appid: %s", app_id); res = MA_ERROR_NONE; @@ -97,6 +107,12 @@ int ma_settings_change_voice_assistant(const char* app_id) { int ma_settings_get_voice_assistant_enabled(const char* app_id, bool *enabled) { bool multiple = false; + + if (!enabled || !app_id) { + LOGE("Invalid parameter"); + return MA_ERROR_INVALID_PARAMETER; + } + int res = ma_settings_is_multiple_mode(&multiple); if (MA_ERROR_NONE != res) return res; @@ -105,11 +121,6 @@ int ma_settings_get_voice_assistant_enabled(const char* app_id, bool *enabled) { return MA_ERROR_NOT_SUPPORTED; } - if (!enabled || !app_id) { - LOGE("Invalid parameter"); - return MA_ERROR_INVALID_PARAMETER; - } - char* enabled_assistants = vconf_get_str(ENABLED_ASSISTANT_URL); if (vconf_get_ext_errno() != VCONF_OK) { LOGE("Operation Failed"); @@ -133,6 +144,11 @@ int ma_settings_get_voice_assistant_enabled(const char* app_id, bool *enabled) { int ma_settings_set_voice_assistant_enabled(const char* app_id, bool enabled) { bool multiple = false; + if (!app_id) { + LOGE("Invalid parameter"); + return MA_ERROR_INVALID_PARAMETER; + } + int res = ma_settings_is_multiple_mode(&multiple); if (MA_ERROR_NONE != res) return res; @@ -140,76 +156,73 @@ int ma_settings_set_voice_assistant_enabled(const char* app_id, bool enabled) { LOGE("Not supported"); return MA_ERROR_NOT_SUPPORTED; } - if (NULL != app_id) { - char* enabled_assistants = vconf_get_str(ENABLED_ASSISTANT_URL); - if (enabled) { - if (NULL != enabled_assistants) { - char* tmp = strstr(enabled_assistants, app_id); - if (tmp) { - LOGE("Not supported"); - res = MA_ERROR_NOT_SUPPORTED; + + char* enabled_assistants = vconf_get_str(ENABLED_ASSISTANT_URL); + if (enabled) { + if (NULL != enabled_assistants) { + char* tmp = strstr(enabled_assistants, app_id); + if (tmp) { + LOGE("Not supported"); + res = MA_ERROR_NOT_SUPPORTED; + } else { + char assistants[MAX_LEN] = ""; + snprintf(assistants, MAX_LEN, "%s%s;", enabled_assistants, app_id); + if (0 == vconf_set_str(ENABLED_ASSISTANT_URL, assistants)) { + res = MA_ERROR_NONE; } else { - char assistants[MAX_LEN] = ""; - snprintf(assistants, MAX_LEN, "%s%s;", enabled_assistants, app_id); - if (0 == vconf_set_str(ENABLED_ASSISTANT_URL, assistants)) { - res = MA_ERROR_NONE; - } else { - LOGE("Operation failed"); - res = MA_ERROR_OPERATION_FAILED; - } + LOGE("Operation failed"); + res = MA_ERROR_OPERATION_FAILED; } - } else { - LOGE("Operation failed"); - res = MA_ERROR_OPERATION_FAILED; } } else { - if (NULL != enabled_assistants) { - char* tmp = strstr(enabled_assistants, app_id); - if (tmp) { - char assistants[MAX_LEN] = ""; - char old_assistants[MAX_LEN] = ""; - /*to prevent memory leak for cut the string enabled_assistants*/ - snprintf(old_assistants, MAX_LEN, "%s", enabled_assistants); - char* rest = old_assistants; - char* as = strtok_r(rest, ";", &rest); - while (as) { - if (strncmp(app_id, as, MAX_LEN)) { - if ((strlen(as) + strlen(assistants) + 1) <= MAX_LEN) { - strncat(assistants, as, strlen(as)); - strncat(assistants, ";", 1); - } else { - LOGE("Operation failed"); - res = MA_ERROR_OPERATION_FAILED; - if (enabled_assistants) { - free(enabled_assistants); - } - return res; + LOGE("Operation failed"); + res = MA_ERROR_OPERATION_FAILED; + } + } else { + if (NULL != enabled_assistants) { + char* tmp = strstr(enabled_assistants, app_id); + if (tmp) { + char assistants[MAX_LEN] = ""; + char old_assistants[MAX_LEN] = ""; + /*to prevent memory leak for cut the string enabled_assistants*/ + snprintf(old_assistants, MAX_LEN, "%s", enabled_assistants); + char* rest = old_assistants; + char* as = strtok_r(rest, ";", &rest); + while (as) { + if (strncmp(app_id, as, MAX_LEN)) { + if ((strlen(as) + strlen(assistants) + 1) <= MAX_LEN) { + strncat(assistants, as, strlen(as)); + strncat(assistants, ";", 1); + } else { + LOGE("Operation failed"); + res = MA_ERROR_OPERATION_FAILED; + if (enabled_assistants) { + free(enabled_assistants); } + return res; } - as = strtok_r(rest, ";", &rest); - } - if (0 == vconf_set_str(ENABLED_ASSISTANT_URL, assistants)) { - res = MA_ERROR_NONE; - } else { - LOGE("Operation failed"); - res = MA_ERROR_OPERATION_FAILED; } + as = strtok_r(rest, ";", &rest); + } + if (0 == vconf_set_str(ENABLED_ASSISTANT_URL, assistants)) { + res = MA_ERROR_NONE; } else { - LOGE("Not support"); - res = MA_ERROR_NOT_SUPPORTED; + LOGE("Operation failed"); + res = MA_ERROR_OPERATION_FAILED; } } else { - LOGE("Operation failed"); - res = MA_ERROR_OPERATION_FAILED; + LOGE("Not support"); + res = MA_ERROR_NOT_SUPPORTED; } + } else { + LOGE("Operation failed"); + res = MA_ERROR_OPERATION_FAILED; } - if (enabled_assistants) { - free(enabled_assistants); - } - } else { - LOGE("Engine not found"); - res = MA_ERROR_ENGINE_NOT_FOUND; } + if (enabled_assistants) { + free(enabled_assistants); + } + return res; } @@ -217,6 +230,11 @@ int ma_settings_get_default_voice_assistant(char** app_id) { bool multiple = false; int res = MA_ERROR_NONE; + if (!app_id) { + LOGE("Invalid parameter"); + return MA_ERROR_INVALID_PARAMETER; + } + res = ma_settings_is_multiple_mode(&multiple); LOGD("multiple mode(%d). Error(%x)", multiple, res); @@ -241,11 +259,16 @@ int ma_settings_set_default_voice_assistant(const char* app_id) { bool multiple = false; int res = MA_ERROR_NONE; + if (!app_id) { + LOGE("Invalid parameter"); + return MA_ERROR_INVALID_PARAMETER; + } + res = ma_settings_is_multiple_mode(&multiple); LOGD("multiple mode(%d). Error(%x)", multiple, res); if (MA_ERROR_NONE != res) return res; - if (multiple && app_id) { + if (multiple) { if (0 == vconf_set_str(DEFAULT_ASSISTANT_URL, app_id)) { LOGI("Succeeded. app id(%s)", app_id); res = MA_ERROR_NONE; diff --git a/include/multi_assistant_settings.h b/include/multi_assistant_settings.h index 715612b..b9e3493 100644 --- a/include/multi_assistant_settings.h +++ b/include/multi_assistant_settings.h @@ -35,6 +35,7 @@ extern "C" * * @return @c 0 on success, otherwise a negative error value * @retval #MA_ERROR_NONE Successful + * @retval #MA_ERROR_INVALID_PARAMETER Invalid parameter * @retval #MA_ERROR_OPERATION_FAILED Operation failed * * @param[out] multiple The current multiple mode of multi-assistant @@ -64,6 +65,7 @@ int ma_settings_set_multiple_mode(bool multiple); * @return @c 0 on success, otherwise a negative error value * @retval #MA_ERROR_NONE Successful * @retval #MA_ERROR_NOT_SUPPORTED Not supported + * @retval #MA_ERROR_INVALID_PARAMETER Invalid parameter * @retval #MA_ERROR_OPERATION_FAILED Operation failed * * @param[out] app_id The app id of the currently activated voice assistant @@ -79,6 +81,7 @@ int ma_settings_get_current_voice_assistant(char** app_id); * @return @c 0 on success, otherwise a negative error value * @retval #MA_ERROR_NONE Successful * @retval #MA_ERROR_NOT_SUPPORTED Not supported + * @retval #MA_ERROR_INVALID_PARAMETER Invalid parameter * @retval #MA_ERROR_OPERATION_FAILED Operation failed * * @param[in] app_id The app id of the voice assistant that need to be activated @@ -110,7 +113,7 @@ int ma_settings_get_voice_assistant_enabled(const char* app_id, bool *enabled); * @return @c 0 on success, otherwise a negative error value * @retval #MA_ERROR_NONE Successful * @retval #MA_ERROR_NOT_SUPPORTED Not supported - * @retval #MA_ERROR_ENGINE_NOT_FOUND Engine not found + * @retval #MA_ERROR_INVALID_PARAMETER Invalid parameter * * @param[in] app_id The app id of the voice assistant that need to be changed * @param[in] enabled The voice assistant need to be enabled or not @@ -127,6 +130,7 @@ int ma_settings_set_voice_assistant_enabled(const char* app_id, bool enabled); * @return @c 0 on success, otherwise a negative error value * @retval #MA_ERROR_NONE Successful * @retval #MA_ERROR_NOT_SUPPORTED Not supported + * @retval #MA_ERROR_INVALID_PARAMETER Invalid parameter * @retval #MA_ERROR_OPERATION_FAILED Operation failed * * @param[out] app_id The app id of the voice assistant currently set as default one @@ -142,6 +146,7 @@ int ma_settings_get_default_voice_assistant(char** app_id); * @return @c 0 on success, otherwise a negative error value * @retval #MA_ERROR_NONE Successful * @retval #MA_ERROR_NOT_SUPPORTED Not supported + * @retval #MA_ERROR_INVALID_PARAMETER Invalid parameter * @retval #MA_ERROR_OPERATION_FAILED Operation failed * * @param[in] app_id The app id of the voice assistant that need to set as default one -- 2.34.1