Fix segmentation fault 80/157980/1
authorsooyeon.kim <sooyeon.kim@samsung.com>
Mon, 18 Sep 2017 11:13:39 +0000 (20:13 +0900)
committerWonnam Jang <wn.jang@samsung.com>
Fri, 27 Oct 2017 06:04:10 +0000 (06:04 +0000)
Change-Id: I19fce1faa2c0e047c40521937506d313609de380
Signed-off-by: sooyeon.kim <sooyeon.kim@samsung.com>
(cherry picked from commit 38d0a62ff0352d6447b9099d1702c41466d51f77)

server/ttsd_data.cpp

index 93bbd50..a7bdfeb 100644 (file)
@@ -71,11 +71,13 @@ int __data_show_sound_list(int index)
        SLOG(LOG_DEBUG, tts_tag(), "----- Sound list -----");
 
        unsigned int i = 0;
-       std::list<sound_data_s*>::iterator iter;
-       for (iter = g_app_list[index].m_wav_data.begin(); iter != g_app_list[index].m_wav_data.end(); ++iter) {
-               SLOG(LOG_DEBUG, tts_tag(), "[%dth][%p] data(%p) data size(%d), uttid(%d), type(%d)", 
-                       i, *iter, (*iter)->data, (*iter)->data_size, (*iter)->utt_id, (*iter)->audio_type);
-               i++;
+       if (!g_app_list[index].m_wav_data.empty()) {
+               std::list<sound_data_s*>::iterator iter;
+               for (iter = g_app_list[index].m_wav_data.begin(); (NULL != *iter && iter != g_app_list[index].m_wav_data.end()); ++iter) {
+                       SLOG(LOG_DEBUG, tts_tag(), "[%dth][%p] data(%p) data size(%ld), uttid(%d), type(%d)", 
+                                       i, *iter, (*iter)->data, (*iter)->data_size, (*iter)->utt_id, (*iter)->audio_type);
+                       i++;
+               }
        }
 
        if (i == 0) {
@@ -91,11 +93,13 @@ int __data_show_text_list(int index)
        SLOG(LOG_DEBUG, tts_tag(), "----- Text list -----");
 
        unsigned int i = 0;
-       std::list<speak_data_s*>::iterator iter;
-       for (iter = g_app_list[index].m_speak_data.begin(); iter != g_app_list[index].m_speak_data.end(); ++iter) {
-               SLOG(LOG_DEBUG, tts_tag(), "[%dth][%p] lang(%s), vctype(%d), speed(%d), uttid(%d), text(%s)", 
-                       i + 1, *iter, (*iter)->lang, (*iter)->vctype, (*iter)->speed, (*iter)->utt_id, (*iter)->text);
-               i++;
+       if (!g_app_list[index].m_speak_data.empty()) {
+               std::list<speak_data_s*>::iterator iter;
+               for (iter = g_app_list[index].m_speak_data.begin(); (NULL != *iter && iter != g_app_list[index].m_speak_data.end()); ++iter) {
+                       SLOG(LOG_DEBUG, tts_tag(), "[%dth][%p] lang(%s), vctype(%d), speed(%d), uttid(%d), text(%s)", 
+                                       i + 1, *iter, (*iter)->lang, (*iter)->vctype, (*iter)->speed, (*iter)->utt_id, (*iter)->text);
+                       i++;
+               }
        }
 
        if (0 == i) {
@@ -111,10 +115,12 @@ int __data_show_used_voice_list(int index)
        SLOG(LOG_DEBUG, tts_tag(), "----- Used voice list -----");
 
        unsigned int i = 0;
-       std::list<used_voice_s>::iterator iter;
-       for (iter = g_app_list[index].m_used_voice.begin(); iter != g_app_list[index].m_used_voice.end(); ++iter) {
-               SLOG(LOG_DEBUG, tts_tag(), "[%dth] lang(%s), vctype(%d)", i + 1, iter->lang, iter->vctype);
-               i++;
+       if (!g_app_list[index].m_used_voice.empty()) {
+               std::list<used_voice_s>::iterator iter;
+               for (iter = g_app_list[index].m_used_voice.begin(); iter != g_app_list[index].m_used_voice.end(); ++iter) {
+                       SLOG(LOG_DEBUG, tts_tag(), "[%dth] lang(%s), vctype(%d)", i + 1, iter->lang, iter->vctype);
+                       i++;
+               }
        }
 
        if (0 == i) {
@@ -238,11 +244,12 @@ int ttsd_data_set_used_voice(int uid, const char* lang, int type)
 
        /* Find voice */
        std::list<used_voice_s>::iterator iter;
-
-       for (iter = g_app_list[index].m_used_voice.begin(); iter != g_app_list[index].m_used_voice.end();++iter) {
-               if (0 == strcmp(lang, iter->lang) && type == iter->vctype) {
-                       SLOG(LOG_DEBUG, tts_tag(), "[DATA] The voice is already registered (%s)(%d)", lang, type);
-                       return 0;
+       if (!g_app_list[index].m_used_voice.empty()) {
+               for (iter = g_app_list[index].m_used_voice.begin(); iter != g_app_list[index].m_used_voice.end();++iter) {
+                       if (0 == strcmp(lang, iter->lang) && type == iter->vctype) {
+                               SLOG(LOG_DEBUG, tts_tag(), "[DATA] The voice is already registered (%s)(%d)", lang, type);
+                               return 0;
+                       }
                }
        }
 
@@ -281,20 +288,22 @@ int ttsd_data_reset_used_voice(int uid, ttsd_used_voice_cb callback)
        }
 
        /* Find voice */
-       std::list<used_voice_s>::iterator iter;
+       if (!g_app_list[index].m_used_voice.empty()) {
+               std::list<used_voice_s>::iterator iter;
 
-       for (iter = g_app_list[index].m_used_voice.begin(); iter != g_app_list[index].m_used_voice.end(); ++iter) {
-               if (NULL != callback) {
-                       callback(iter->lang, iter->vctype);
-               }
+               for (iter = g_app_list[index].m_used_voice.begin(); iter != g_app_list[index].m_used_voice.end(); ++iter) {
+                       if (NULL != callback) {
+                               callback(iter->lang, iter->vctype);
+                       }
 
-               if (NULL != iter->lang) {
-                       free(iter->lang);
-                       iter->lang = NULL;
+                       if (NULL != iter->lang) {
+                               free(iter->lang);
+                               iter->lang = NULL;
+                       }
                }
-       }
 
-       g_app_list[index].m_used_voice.clear();
+               g_app_list[index].m_used_voice.clear();
+       }
 
 #ifdef DATA_DEBUG
        __data_show_used_voice_list(index);
@@ -361,10 +370,11 @@ int ttsd_data_get_speak_data(int uid, speak_data_s** data)
                return -1;
        }
 
-       std::list<speak_data_s*>::iterator iter = g_app_list[index].m_speak_data.begin();
-       *data = *iter;
-       if (!g_app_list[index].m_speak_data.empty())
+       if (!g_app_list[index].m_speak_data.empty()) {
+               std::list<speak_data_s*>::iterator iter = g_app_list[index].m_speak_data.begin();
+               *data = *iter;
                g_app_list[index].m_speak_data.pop_front();
+       }
 
 #ifdef DATA_DEBUG
        __data_show_text_list(index);
@@ -435,10 +445,11 @@ int ttsd_data_get_sound_data(int uid, sound_data_s** data)
                return -1;
        }
 
-       std::list<sound_data_s*>::iterator iter = g_app_list[index].m_wav_data.begin();
-       *data = *iter;
-       if (!g_app_list[index].m_wav_data.empty())
+       if (!g_app_list[index].m_wav_data.empty()) {
+               std::list<sound_data_s*>::iterator iter = g_app_list[index].m_wav_data.begin();
+               *data = *iter;
                g_app_list[index].m_wav_data.pop_front();
+       }
 
 #ifdef DATA_DEBUG
        __data_show_sound_list(index);
@@ -491,6 +502,7 @@ int ttsd_data_clear_data(int uid)
                        break;
                }
 
+               pthread_mutex_lock(&g_speak_data_mutex);
                if (NULL != temp_speak) {
                        SLOG(LOG_DEBUG, tts_tag(), "[DEBUG] utt(%d), text(%s), lang(%s), vctype(%d) speed(%d)", 
                                        temp_speak->utt_id, temp_speak->text, temp_speak->lang, temp_speak->vctype, temp_speak->speed);
@@ -508,17 +520,23 @@ int ttsd_data_clear_data(int uid)
                        free(temp_speak);
                        temp_speak = NULL;
                }
+               pthread_mutex_unlock(&g_speak_data_mutex);
        }
 
        if (-1 != removed_last_uttid) {
                g_app_list[index].utt_id_stopped = removed_last_uttid;
        }
 
+       pthread_mutex_lock(&g_speak_data_mutex);
+       g_app_list[index].m_speak_data.clear();
+       pthread_mutex_unlock(&g_speak_data_mutex);
+
        while(1) {
                if (0 != ttsd_data_get_sound_data(uid, &temp_sound)) {
                        break;
                }
 
+               pthread_mutex_lock(&g_sound_data_mutex);
                if (NULL != temp_sound) {
                        SLOG(LOG_ERROR, tts_tag(), "[DEBUG][%p] uid(%d), event(%d) data(%p) size(%d) rate(%d) utt(%d)", 
                                temp_sound, uid, temp_sound->event, temp_sound->data, temp_sound->data_size, temp_sound->rate, temp_sound->utt_id);
@@ -531,12 +549,9 @@ int ttsd_data_clear_data(int uid)
                        free(temp_sound);
                        temp_sound = NULL;
                }
+               pthread_mutex_unlock(&g_sound_data_mutex);
        }
 
-       pthread_mutex_lock(&g_speak_data_mutex);
-       g_app_list[index].m_speak_data.clear();
-       pthread_mutex_unlock(&g_speak_data_mutex);
-
        pthread_mutex_lock(&g_sound_data_mutex);
        g_app_list[index].m_wav_data.clear();
        pthread_mutex_unlock(&g_sound_data_mutex);
@@ -715,11 +730,13 @@ int ttsd_data_save_error_log(int uid, FILE* fp)
        fprintf(fp, "----- Sound list -----");
 
        i = 0;
-       std::list<sound_data_s*>::iterator iter;
-       for (iter = g_app_list[index].m_wav_data.begin(); iter != g_app_list[index].m_wav_data.end(); ++iter) {
-               SLOG(LOG_DEBUG, tts_tag(), "[%dth][%p] data(%p) data size(%d), uttid(%d), type(%d)",
-                       i, *iter, (*iter)->data, (*iter)->data_size, (*iter)->utt_id, (*iter)->audio_type);
-               i++;
+       if (!g_app_list[index].m_wav_data.empty()) {
+               std::list<sound_data_s*>::iterator iter;
+               for (iter = g_app_list[index].m_wav_data.begin(); (NULL != *iter && iter != g_app_list[index].m_wav_data.end()); ++iter) {
+                       SLOG(LOG_DEBUG, tts_tag(), "[%dth][%p] data(%p) data size(%ld), uttid(%d), type(%d)",
+                                       i, *iter, (*iter)->data, (*iter)->data_size, (*iter)->utt_id, (*iter)->audio_type);
+                       i++;
+               }
        }
 
        fprintf(fp, "----------------------");
@@ -728,11 +745,13 @@ int ttsd_data_save_error_log(int uid, FILE* fp)
        fprintf(fp, "----- Text list -----");
 
        i = 0;
-       std::list<speak_data_s*>::iterator iter_speak;
-       for (iter_speak = g_app_list[index].m_speak_data.begin(); iter_speak != g_app_list[index].m_speak_data.end(); ++iter_speak) {
-               SLOG(LOG_DEBUG, tts_tag(), "[%dth][%p] lang(%s), vctype(%d), speed(%d), uttid(%d), text(%s)",
-                       i, *iter_speak, (*iter_speak)->lang, (*iter_speak)->vctype, (*iter_speak)->speed, (*iter_speak)->utt_id, (*iter_speak)->text);
-               i++;
+       if (!g_app_list[index].m_speak_data.empty()) {
+               std::list<speak_data_s*>::iterator iter_speak;
+               for (iter_speak = g_app_list[index].m_speak_data.begin(); (NULL != *iter_speak && iter_speak != g_app_list[index].m_speak_data.end()); ++iter_speak) {
+                       SLOG(LOG_DEBUG, tts_tag(), "[%dth][%p] lang(%s), vctype(%d), speed(%d), uttid(%d), text(%s)",
+                                       i, *iter_speak, (*iter_speak)->lang, (*iter_speak)->vctype, (*iter_speak)->speed, (*iter_speak)->utt_id, (*iter_speak)->text);
+                       i++;
+               }
        }
        fprintf(fp, "---------------------");