Fix invalid file pointer access to avoid crash 11/267011/3
authorSuyeon Hwang <stom.hwang@samsung.com>
Wed, 24 Nov 2021 05:41:32 +0000 (14:41 +0900)
committerSuyeon Hwang <stom.hwang@samsung.com>
Mon, 29 Nov 2021 01:32:21 +0000 (10:32 +0900)
Change-Id: I71ce23eb03e181a5157bddbccfe18221e349d01c
Signed-off-by: Suyeon Hwang <stom.hwang@samsung.com>
server/ttsd_player.c

index 76a8c8b8ce721097d6471287b74ecd52f0e34fb3..87cebe865e834c4c2ffe7d3c2eb6af3e77215012 100644 (file)
@@ -60,12 +60,12 @@ static const int EXTRA_INFO_LENGTH = 20;
 /*
 #define BUF_SAVE_MODE
 */
+
 #ifdef BUF_SAVE_MODE
 static char g_temp_file_name[128] = {'\0',};
-
 static FILE* g_pFile;
-
-static int g_count = 1;
+static int g_count = 0;
+static pthread_mutex_t g_buf_save_mutex = PTHREAD_MUTEX_INITIALIZER;
 #endif
 
 /** player init info */
@@ -115,6 +115,72 @@ static pthread_mutex_t g_player_control_mutex = PTHREAD_MUTEX_INITIALIZER;
 * Internal Interfaces
 */
 
+#ifdef BUF_SAVE_MODE
+static void __open_buffer_dump_file()
+{
+       pthread_mutex_lock(&g_buf_save_mutex);
+       if (g_pFile) {
+               SLOG(LOG_ERROR, tts_tag(), "[Buffer Dump] File is already opened(%s)", g_temp_file_name);
+               pthread_mutex_unlock(&g_buf_save_mutex);
+               return;
+       }
+
+       g_count++;
+       while (1) {
+               snprintf(g_temp_file_name, sizeof(g_temp_file_name), "/tmp/tts_temp_%d_%d", getpid(), g_count);
+               int ret = access(g_temp_file_name, 0);
+
+               if (0 == ret) {
+                       SLOG(LOG_ERROR, tts_tag(), "[Recorder ERROR] File is already exist");
+                       if (0 == remove(g_temp_file_name)) {
+                               SLOG(LOG_DEBUG, tts_tag(), "[Recorder] Remove file");
+                               break;
+                       } else {
+                               g_count++;
+                       }
+               } else {
+                       break;
+               }
+       }
+
+       SECURE_SLOG(LOG_DEBUG, tts_tag(), "[Recorder] Temp file name=[%s]", g_temp_file_name);
+
+       /* open test file */
+       g_pFile = fopen(g_temp_file_name, "wb+x");
+       if (NULL == g_pFile) {
+               SLOG(LOG_ERROR, tts_tag(), "[Recorder ERROR] File not found!");
+       }
+
+       pthread_mutex_unlock(&g_buf_save_mutex);
+}
+
+static void __close_buffer_dump_file()
+{
+       pthread_mutex_lock(&g_buf_save_mutex);
+
+       if (g_pFile) {
+               fclose(g_pFile);
+               g_pFile = NULL;
+       }
+
+       pthread_mutex_unlock(&g_buf_save_mutex);
+}
+
+static void __write_buffer_dump_file(const void* buffer, size_t length)
+{
+       pthread_mutex_lock(&g_buf_save_mutex);
+
+       if (g_pFile) {
+               size_t ret = fwrite(buffer, 1, length, g_pFile);
+               SLOG(LOG_DEBUG, tts_tag(), "[Buffer Dump] Stored size(%zu / %zu)", ret, length);
+       } else {
+               SLOG(LOG_ERROR, tts_tag(), "[Buffer Dump] File is not opened. Please check the file open");
+       }
+
+       pthread_mutex_unlock(&g_buf_save_mutex);
+}
+#endif
+
 static bool __is_player_valid(player_s* player)
 {
        if (NULL == player || NULL == g_playing_info) {
@@ -365,10 +431,6 @@ static int __destroy_audio_out()
 static void __end_play_thread(void *data, Ecore_Thread *thread)
 {
        SLOG(LOG_ERROR, tts_tag(), "@@@ End thread");
-
-#ifdef BUF_SAVE_MODE
-       fclose(g_pFile);
-#endif
 }
 
 static void __del_timer_for_delayed_recover(void* data)
@@ -511,36 +573,6 @@ static void __play_thread(void *data, Ecore_Thread *thread)
 {
        SLOG(LOG_DEBUG, tts_tag(), "@@@ Start thread");
 
-#ifdef BUF_SAVE_MODE
-       g_count++;
-
-       while (1) {
-               snprintf(g_temp_file_name, sizeof(g_temp_file_name), "/tmp/tts_temp_%d_%d", getpid(), g_count);
-               int ret = access(g_temp_file_name, 0);
-
-               if (0 == ret) {
-                       SLOG(LOG_ERROR, tts_tag(), "[Recorder ERROR] File is already exist");
-                       if (0 == remove(g_temp_file_name)) {
-                               SLOG(LOG_DEBUG, tts_tag(), "[Recorder] Remove file");
-                               break;
-                       } else {
-                               g_count++;
-                       }
-               } else {
-                       break;
-               }
-       }
-
-       SECURE_SLOG(LOG_DEBUG, tts_tag(), "[Recorder] Temp file name=[%s]", g_temp_file_name);
-
-       /* open test file */
-       g_pFile = fopen(g_temp_file_name, "wb+x");
-       if (!g_pFile) {
-               SLOG(LOG_ERROR, tts_tag(), "[Recorder ERROR] File not found!");
-               return;
-       }
-#endif
-
        if (NULL == g_playing_info) {
                SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] No current player");
                return;
@@ -707,6 +739,10 @@ static void __play_thread(void *data, Ecore_Thread *thread)
                                        return;
                                }
 
+#ifdef BUF_SAVE_MODE
+                                       __open_buffer_dump_file();
+#endif
+
                                if (0 != ttsdc_ipc_send_utt_start_message(pid, player->uid, sound_data->utt_id)) {
                                        SLOG(LOG_ERROR, tts_tag(), "[Send ERROR] Fail to send Utterance Start Signal : pid(%d), uid(%d), uttid(%d)",
                                                pid, player->uid, sound_data->utt_id);
@@ -735,6 +771,10 @@ static void __play_thread(void *data, Ecore_Thread *thread)
 
                                        __unset_policy_for_playing();
 
+#ifdef BUF_SAVE_MODE
+                                       __close_buffer_dump_file();
+#endif
+
                                        if (0 != ttsdc_ipc_send_utt_finish_message(pid, player->uid, sound_data->utt_id)) {
                                                SLOG(LOG_ERROR, tts_tag(), "[Send ERROR] Fail to send Utterance Completed Signal : pid(%d), uid(%d), uttid(%d)",
                                                        pid, player->uid, sound_data->utt_id);
@@ -811,8 +851,7 @@ static void __play_thread(void *data, Ecore_Thread *thread)
                        SLOG(LOG_INFO, tts_tag(), "[Player INFO] Before audio_out_write. data(%p), data[%d](%p), uid(%d), utt_id(%d), len(%d)",
                                        temp_data, idx, &temp_data[idx], player->uid, sound_data->utt_id, len);
 #ifdef BUF_SAVE_MODE
-                       /* write pcm buffer */
-                       fwrite(&temp_data[idx], 1, len, g_pFile);
+                       __write_buffer_dump_file(&temp_data[idx], len);
 #endif
                        ret = audio_out_write(g_audio_h, &temp_data[idx], len);
                        if (0 > ret) {
@@ -900,6 +939,9 @@ static void __play_thread(void *data, Ecore_Thread *thread)
                                return;
                        }
 
+#ifdef BUF_SAVE_MODE
+                       __close_buffer_dump_file();
+#endif
                        if (0 != ttsdc_ipc_send_utt_finish_message(pid, player->uid, sound_data->utt_id)) {
                                SLOG(LOG_ERROR, tts_tag(), "[Send ERROR] Fail to send Utterance Completed Signal : pid(%d), uid(%d), uttid(%d)",
                                        pid, player->uid, sound_data->utt_id);
@@ -1068,6 +1110,10 @@ static void __destroy_all_ducking_handles()
 
 int ttsd_player_release(void)
 {
+#ifdef BUF_SAVE_MODE
+       __close_buffer_dump_file();
+#endif
+
        pthread_mutex_lock(&g_player_control_mutex);
        if (false == g_player_init) {
                SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
@@ -1278,6 +1324,10 @@ int ttsd_player_stop(int uid)
                pthread_mutex_unlock(&g_play_thread_mutex);
        }
 
+#ifdef BUF_SAVE_MODE
+       __close_buffer_dump_file();
+#endif
+
        int ret = ttsd_player_clear(uid);
        if (0 != ret) {
                SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to stop player, ret(%d)", ret);
@@ -1362,6 +1412,10 @@ int ttsd_player_pause(int uid)
                pthread_mutex_unlock(&g_play_thread_mutex);
        }
 
+#ifdef BUF_SAVE_MODE
+       __close_buffer_dump_file();
+#endif
+
        SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Pause player : uid(%d)", uid);
 
        pthread_mutex_unlock(&g_player_control_mutex);