Get UID from list using tts_config_client_s structure 94/268294/4
authorSuyeon Hwang <stom.hwang@samsung.com>
Mon, 20 Dec 2021 04:53:26 +0000 (13:53 +0900)
committerSuyeon Hwang <stom.hwang@samsung.com>
Mon, 27 Dec 2021 06:30:03 +0000 (15:30 +0900)
Current code converts the type of element as integer. However, the element of the list is instance
of tts_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: I168011529efec4a59a1a1abc8b7d203e412a4972
Signed-off-by: Suyeon Hwang <stom.hwang@samsung.com>
common/tts_config_mgr.c

index 15e57410ad0be65392f4010e932f4ca49a2cd0c6..8a208732af3576cb078e368500ca59a74a1f0200 100644 (file)
@@ -1133,65 +1133,63 @@ static int __tts_config_mgr_unregister_engine_config_updated_event()
 
 int tts_config_mgr_initialize(unsigned int uid, tts_config_client_type_e client_type)
 {
-       GSList *iter = NULL;
-       int* get_uid;
-       tts_config_client_s* temp_client = NULL;
+       tts_config_client_s* new_client = NULL;
 
        /* Register uid */
        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;
+                       tts_config_client_s* client = (tts_config_client_s*)iter->data;
 
-                       if (uid == *get_uid) {
+                       if (NULL != client && uid == client->uid) {
                                SECURE_SLOG(LOG_WARN, TAG_TTSCONFIG, "[CONFIG] uid(%u) has already registered", uid);
-                               return 0;
+                               return TTS_CONFIG_ERROR_NONE;
                        }
 
                        iter = g_slist_next(iter);
                }
 
-               temp_client = (tts_config_client_s*)calloc(1, sizeof(tts_config_client_s));
-               if (NULL == temp_client) {
+               new_client = (tts_config_client_s*)calloc(1, sizeof(tts_config_client_s));
+               if (NULL == new_client) {
                        SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to allocate memory");
                        return TTS_CONFIG_ERROR_OUT_OF_MEMORY;
                }
-               temp_client->uid = uid;
-               temp_client->engine_cb = NULL;
-               temp_client->voice_cb = NULL;
-               temp_client->speech_cb = NULL;
-               temp_client->pitch_cb = NULL;
-               temp_client->screen_cb = NULL;
-               temp_client->bg_volume_ratio_cb = NULL;
-               temp_client->user_data = NULL;
-               temp_client->screen_user_data = NULL;
+               new_client->uid = uid;
+               new_client->engine_cb = NULL;
+               new_client->voice_cb = NULL;
+               new_client->speech_cb = NULL;
+               new_client->pitch_cb = NULL;
+               new_client->screen_cb = NULL;
+               new_client->bg_volume_ratio_cb = NULL;
+               new_client->user_data = NULL;
+               new_client->screen_user_data = NULL;
 
-               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);
 
                SECURE_SLOG(LOG_WARN, TAG_TTSCONFIG, "[CONFIG] Add uid(%u) but config has already initialized", uid);
                SLOG(LOG_INFO, TAG_TTSCONFIG, "Client type : %d", client_type);
                g_client_type |= client_type;
 
-               return 0;
+               return TTS_CONFIG_ERROR_NONE;
        } else {
-               temp_client = (tts_config_client_s*)calloc(1, sizeof(tts_config_client_s));
-               if (NULL == temp_client) {
+               new_client = (tts_config_client_s*)calloc(1, sizeof(tts_config_client_s));
+               if (NULL == new_client) {
                        SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to allocate memory");
                        return TTS_CONFIG_ERROR_OUT_OF_MEMORY;
                }
-               temp_client->uid = uid;
-               temp_client->engine_cb = NULL;
-               temp_client->voice_cb = NULL;
-               temp_client->speech_cb = NULL;
-               temp_client->pitch_cb = NULL;
-               temp_client->screen_cb = NULL;
-               temp_client->bg_volume_ratio_cb = NULL;
-               temp_client->user_data = NULL;
-               temp_client->screen_user_data = NULL;
-
-               g_config_client_list = g_slist_append(g_config_client_list, temp_client);
+               new_client->uid = uid;
+               new_client->engine_cb = NULL;
+               new_client->voice_cb = NULL;
+               new_client->speech_cb = NULL;
+               new_client->pitch_cb = NULL;
+               new_client->screen_cb = NULL;
+               new_client->bg_volume_ratio_cb = NULL;
+               new_client->user_data = NULL;
+               new_client->screen_user_data = NULL;
+
+               g_config_client_list = g_slist_append(g_config_client_list, new_client);
        }
 
        if (0 != access(TTS_CONFIG_BASE, F_OK)) {