Add invalid parameter check in setting APIs 96/280896/5
authorJihoon Kim <jihoon48.kim@samsung.com>
Tue, 6 Sep 2022 10:13:57 +0000 (19:13 +0900)
committerJihoon Kim <jihoon48.kim@samsung.com>
Wed, 7 Sep 2022 02:04:54 +0000 (11:04 +0900)
Change-Id: Ibdd6336b50403124d65248d45f41a85ac675629a
Signed-off-by: Jihoon Kim <jihoon48.kim@samsung.com>
common/multi_assistant_settings.c
include/multi_assistant_settings.h

index fb1b1a3b29d12777cd1564398ff4e872933475af..3a9f3765fe6601f91d663a4adaaaa519869c4ad4 100644 (file)
@@ -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;
index 715612baa5c5008b9c37cc7967ee531eb94e55bb..b9e3493d85f5258460b50844df5689d7038e4956 100644 (file)
@@ -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