Get UID from list using stt_config_client_s structure 84/268284/1
authorSuyeon Hwang <stom.hwang@samsung.com>
Mon, 20 Dec 2021 03:12:01 +0000 (12:12 +0900)
committerSuyeon Hwang <stom.hwang@samsung.com>
Mon, 20 Dec 2021 03:12:04 +0000 (12:12 +0900)
Current code converts the type of element as integer. However, the element of the list is instance
of stt_config_client_s structure. Because the first member of the structure is uid, so current code
has no problem until now. But this can occur problem if the type of uid or the order of member is
changed.

To solve this problem, this patch changes the type from integer to the structure. This can provide
safe access to member.

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

index 19b326d..3f3555b 100644 (file)
@@ -698,38 +698,36 @@ static void __get_engine_list(const char* directory)
 
 int stt_config_mgr_initialize(unsigned int uid)
 {
-       GSList *iter = NULL;
-       int* get_uid;
-       stt_config_client_s* temp_client = NULL;
+       stt_config_client_s* new_client = NULL;
 
        if (0 < g_slist_length(g_config_client_list)) {
                /* Check uid */
-               iter = g_slist_nth(g_config_client_list, 0);
+               GSList *iter = g_slist_nth(g_config_client_list, 0);
 
                while (NULL != iter) {
-                       get_uid = iter->data;
+                       stt_config_client_s* client = (stt_config_client_s*)iter->data;
 
-                       if (uid == *get_uid) {
+                       if (NULL != client && uid == client->uid) {
                                SLOG(LOG_WARN, TAG_STTCONFIG, "[CONFIG] uid(%u) has already registered", uid);
-                               return 0;
+                               return STT_CONFIG_ERROR_NONE;
                        }
 
                        iter = g_slist_next(iter);
                }
 
-               temp_client = (stt_config_client_s*)calloc(1, sizeof(stt_config_client_s));
-               if (NULL == temp_client) {
+               new_client = (stt_config_client_s*)calloc(1, sizeof(stt_config_client_s));
+               if (NULL == new_client) {
                        SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Fail to allocate memory");
                        return STT_CONFIG_ERROR_OUT_OF_MEMORY;
                }
-               temp_client->uid = uid;
-               temp_client->bool_cb = NULL;
-               temp_client->engine_cb = NULL;
-               temp_client->lang_cb = NULL;
-               temp_client->user_data = NULL;
+               new_client->uid = uid;
+               new_client->bool_cb = NULL;
+               new_client->engine_cb = NULL;
+               new_client->lang_cb = NULL;
+               new_client->user_data = NULL;
 
                /* Add uid */
-               g_config_client_list = g_slist_append(g_config_client_list, temp_client);
+               g_config_client_list = g_slist_append(g_config_client_list, new_client);
 
                SLOG(LOG_WARN, TAG_STTCONFIG, "[CONFIG] Add uid(%u) but config has already initialized", uid);
                return STT_CONFIG_ERROR_NONE;
@@ -852,19 +850,19 @@ int stt_config_mgr_initialize(unsigned int uid)
        /* Register to detect display language change */
        vconf_notify_key_changed(VCONFKEY_LANGSET, __stt_config_language_changed_cb, NULL);
 
-       temp_client = (stt_config_client_s*)calloc(1, sizeof(stt_config_client_s));
-       if (NULL == temp_client) {
+       new_client = (stt_config_client_s*)calloc(1, sizeof(stt_config_client_s));
+       if (NULL == new_client) {
                SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Fail to allocate memory"); //LCOV_EXCL_LINE
                return STT_CONFIG_ERROR_OUT_OF_MEMORY;
        }
-       temp_client->uid = uid;
-       temp_client->bool_cb = NULL;
-       temp_client->engine_cb = NULL;
-       temp_client->lang_cb = NULL;
-       temp_client->user_data = NULL;
+       new_client->uid = uid;
+       new_client->bool_cb = NULL;
+       new_client->engine_cb = NULL;
+       new_client->lang_cb = NULL;
+       new_client->user_data = NULL;
 
        /* Add uid */
-       g_config_client_list = g_slist_append(g_config_client_list, temp_client);
+       g_config_client_list = g_slist_append(g_config_client_list, new_client);
 
        return STT_CONFIG_ERROR_NONE;
 }