Change timing of initializing and cleanning up libxml Parser 85/315385/1
authordyamy-lee <dyamy.lee@samsung.com>
Wed, 27 Nov 2024 08:19:56 +0000 (17:19 +0900)
committerdyamy-lee <dyamy.lee@samsung.com>
Mon, 2 Dec 2024 01:28:55 +0000 (10:28 +0900)
Issue : crash when xmlParseFile is called

Solution : The function xmlParseFile() parses XML files.
If the internal of libxml2 is corrupted due to missing initialization, a crash can be occurred.
But, xmlInitParser is a global initialization function that only need to be called once for the entire program.
Therefore, this patch modified the structure to manage initialization before any xmlParseFile usage.

Change-Id: Ie7fc14e4d923e2dd72d938f0f4af349359f13365

common/tts_config_mgr.c
common/tts_config_parser.c
common/tts_config_parser.h

index f95a228c8f59c79ade278e12a3801ff63c1fc00c..c487c6304341e6fa2bca1aa37ece6f6c2a0623e9 100644 (file)
@@ -770,8 +770,12 @@ static Eina_Bool tts_config_mgr_inotify_event_cb(void* data, Ecore_Fd_Handler *f
                SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] IN_DELETE_SELF event");
 
                tts_parser_unload_config();
+               tts_parser_cleanup_xml();
                tts_parser_reset();
-               tts_parser_load_config();
+               tts_parser_initialize_xml();
+               if (0 != tts_parser_load_config()) {
+                       tts_parser_cleanup_xml();
+               }
        } else {
                SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Undefined event (0x%x)", event.mask);
        }
@@ -1328,10 +1332,13 @@ int tts_config_mgr_initialize(unsigned int uid, tts_config_client_type_e client_
        SLOG(LOG_INFO, TAG_TTSCONFIG, "Client type : %d", client_type);
        g_client_type |= client_type;
 
+       tts_parser_initialize_xml();
+
        if (0 != __tts_config_mgr_get_engine_info()) {
                SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to get engine info");
                release_config_client(uid);
                __tts_config_release_engine();
+               tts_parser_cleanup_xml();
                return TTS_CONFIG_ERROR_ENGINE_NOT_FOUND;
        }
 
@@ -1341,6 +1348,7 @@ int tts_config_mgr_initialize(unsigned int uid, tts_config_client_type_e client_
                SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to parse configure information");
                release_config_client(uid);
                __tts_config_release_engine();
+               tts_parser_cleanup_xml();
                return TTS_CONFIG_ERROR_OPERATION_FAILED;
        }
        tts_config_s config_info;
@@ -1348,6 +1356,8 @@ int tts_config_mgr_initialize(unsigned int uid, tts_config_client_type_e client_
                SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to get configure information");
                release_config_client(uid);
                __tts_config_release_engine();
+               tts_parser_unload_config();
+               tts_parser_cleanup_xml();
                return TTS_CONFIG_ERROR_OPERATION_FAILED;
        }
 
@@ -1357,6 +1367,7 @@ int tts_config_mgr_initialize(unsigned int uid, tts_config_client_type_e client_
                release_config_client(uid);
                __tts_config_release_engine();
                tts_parser_unload_config();
+               tts_parser_cleanup_xml();
                return TTS_CONFIG_ERROR_ENGINE_NOT_FOUND;
        }
 
@@ -1373,6 +1384,7 @@ int tts_config_mgr_initialize(unsigned int uid, tts_config_client_type_e client_
                                release_config_client(uid);
                                __tts_config_release_engine();
                                tts_parser_unload_config();
+                               tts_parser_cleanup_xml();
                                return TTS_CONFIG_ERROR_OPERATION_FAILED;
                        }
 
@@ -1388,6 +1400,7 @@ int tts_config_mgr_initialize(unsigned int uid, tts_config_client_type_e client_
                                                release_config_client(uid);
                                                __tts_config_release_engine();
                                                tts_parser_unload_config();
+                                               tts_parser_cleanup_xml();
                                                return TTS_CONFIG_ERROR_OPERATION_FAILED;
                                        }
                                } else {
@@ -1419,6 +1432,7 @@ int tts_config_mgr_initialize(unsigned int uid, tts_config_client_type_e client_
                release_config_client(uid);
                __tts_config_release_engine();
                tts_parser_unload_config();
+               tts_parser_cleanup_xml();
                return TTS_CONFIG_ERROR_OPERATION_FAILED;
        }
 
@@ -1454,6 +1468,7 @@ int tts_config_mgr_finalize(unsigned int uid, tts_config_client_type_e client_ty
        __tts_config_release_engine();
 
        tts_parser_unload_config();
+       tts_parser_cleanup_xml();
 
        __tts_config_mgr_unregister_engine_config_updated_event();
 
index dad135e7518f4a8d074048f445db593131cd2900..2813a6430b7c8ddd20b2360caae01b10ad4f4044 100644 (file)
@@ -55,6 +55,23 @@ char g_language[128] = {0,};
 static tts_config_s* g_config_info = NULL;
 
 static pthread_mutex_t g_config_info_mutex = PTHREAD_MUTEX_INITIALIZER;
+static bool is_libxml_initialized = false;
+
+void tts_parser_initialize_xml(void) {
+       if (!is_libxml_initialized) {
+               SLOG(LOG_ERROR, TAG_TTSCONFIG, "[DEBUG] Initialize xmlParser");
+               xmlInitParser();
+               is_libxml_initialized = true;
+       }
+}
+
+void tts_parser_cleanup_xml(void) {
+       if (is_libxml_initialized) {
+               SLOG(LOG_ERROR, TAG_TTSCONFIG, "[DEBUG] Cleanup xmlParser");
+               xmlCleanupParser();
+               is_libxml_initialized = false;
+       }
+}
 
 int tts_parser_get_engine_info(const char* path, tts_engine_info_s** engine_info)
 {
@@ -360,14 +377,10 @@ int tts_parser_load_config(void)
        xmlNodePtr cur = NULL;
        bool is_default_open = false;
 
-       /* For Thread safety */
-       xmlInitParser();
-
        if (0 != access(TTS_CONFIG, F_OK)) {
                doc = xmlParseFile(TTS_DEFAULT_CONFIG);
                if (doc == NULL) {
                        SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to parse file error : %s", TTS_DEFAULT_CONFIG);
-                       xmlCleanupParser();
                        return -1;
                }
                is_default_open = true;
@@ -387,7 +400,6 @@ int tts_parser_load_config(void)
                                doc = xmlParseFile(TTS_DEFAULT_CONFIG);
                                if (NULL == doc) {
                                        SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to parse file error : %s", TTS_DEFAULT_CONFIG);
-                                       xmlCleanupParser();
                                        return -1;
                                }
                                is_default_open = true;
@@ -401,7 +413,6 @@ int tts_parser_load_config(void)
                SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Empty document(%p)", doc);
                xmlFreeDoc(doc);
                doc = NULL;
-               xmlCleanupParser();
                return -1;
        }
 
@@ -409,7 +420,6 @@ int tts_parser_load_config(void)
                SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] The wrong type, root node is NOT %s. doc(%p)", TTS_TAG_CONFIG_BASE_TAG, doc);
                xmlFreeDoc(doc);
                doc = NULL;
-               xmlCleanupParser();
                return -1;
        }
 
@@ -418,7 +428,6 @@ int tts_parser_load_config(void)
                SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Empty document(%p)", doc);
                xmlFreeDoc(doc);
                doc = NULL;
-               xmlCleanupParser();
                return -1;
        }
 
@@ -429,7 +438,6 @@ int tts_parser_load_config(void)
                SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Out of memory(%p)", doc);
                xmlFreeDoc(doc);
                doc = NULL;
-               xmlCleanupParser();
                return -1;
        }
 
@@ -598,8 +606,6 @@ int tts_parser_unload_config(void)
                g_config_info = NULL;
        }
        pthread_mutex_unlock(&g_config_info_mutex);
-
-       xmlCleanupParser();
        return 0;
 }
 
index a39d935b2ab1ed7b8b9d306798f0434b8d055742..afc078e8bedc9ee1d3c898a42182d72968d13f23 100644 (file)
@@ -91,6 +91,10 @@ int tts_parser_copy_xml(const char* original, const char* destination);
 
 int tts_parser_reset();
 
+void tts_parser_initialize_xml(void);
+
+void tts_parser_cleanup_xml(void);
+
 #ifdef __cplusplus
 }
 #endif