Add null checker when memory is released
[platform/core/uifw/tts.git] / common / tts_config_parser.c
old mode 100644 (file)
new mode 100755 (executable)
index 946bec5..f9f5f8d
@@ -61,6 +61,7 @@ int tts_parser_get_engine_info(const char* path, tts_engine_info_s** engine_info
 
        doc = xmlParseFile(path);
        if (doc == NULL) {
+               SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to parse xml file");
                return -1;
        }
 
@@ -68,12 +69,14 @@ int tts_parser_get_engine_info(const char* path, tts_engine_info_s** engine_info
        if (cur == NULL) {
                SLOG(LOG_ERROR, tts_tag(), "[ERROR] Empty document");
                xmlFreeDoc(doc);
+               doc = NULL;
                return -1;
        }
 
        if (xmlStrcmp(cur->name, (const xmlChar *)TTS_TAG_ENGINE_BASE_TAG)) {
                SLOG(LOG_ERROR, tts_tag(), "[ERROR] The wrong type, root node is NOT 'tts-engine'");
                xmlFreeDoc(doc);
+               doc = NULL;
                return -1;
        }
 
@@ -81,6 +84,7 @@ int tts_parser_get_engine_info(const char* path, tts_engine_info_s** engine_info
        if (cur == NULL) {
                SLOG(LOG_ERROR, tts_tag(), "[ERROR] Empty document");
                xmlFreeDoc(doc);
+               doc = NULL;
                return -1;
        }
 
@@ -103,7 +107,10 @@ int tts_parser_get_engine_info(const char* path, tts_engine_info_s** engine_info
                if (0 == xmlStrcmp(cur->name, (const xmlChar *)TTS_TAG_ENGINE_NAME)) {
                        key = xmlNodeGetContent(cur);
                        if (NULL != key) {
-                               if (NULL != temp->name) free(temp->name);
+                               if (NULL != temp->name) {
+                                       free(temp->name);
+                                       temp->name = NULL;
+                               }
                                temp->name = strdup((char*)key);
                                xmlFree(key);
                        } else {
@@ -112,7 +119,10 @@ int tts_parser_get_engine_info(const char* path, tts_engine_info_s** engine_info
                } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)TTS_TAG_ENGINE_ID)) {
                        key = xmlNodeGetContent(cur);
                        if (NULL != key) {
-                               if (NULL != temp->uuid) free(temp->uuid);
+                               if (NULL != temp->uuid) {
+                                       free(temp->uuid);
+                                       temp->uuid = NULL;
+                               }
                                temp->uuid = strdup((char*)key);
                                xmlFree(key);
                        } else {
@@ -121,7 +131,10 @@ int tts_parser_get_engine_info(const char* path, tts_engine_info_s** engine_info
                } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)TTS_TAG_ENGINE_SETTING)) {
                        key = xmlNodeGetContent(cur);
                        if (NULL != key) {
-                               if (NULL != temp->setting)      free(temp->setting);
+                               if (NULL != temp->setting) {
+                                       free(temp->setting);
+                                       temp->setting = NULL;
+                               }
                                temp->setting = strdup((char*)key);
                                xmlFree(key);
                        } else {
@@ -133,7 +146,6 @@ int tts_parser_get_engine_info(const char* path, tts_engine_info_s** engine_info
 
                        while (NULL != voice_node) {
                                if (0 == xmlStrcmp(voice_node->name, (const xmlChar *)TTS_TAG_ENGINE_VOICE)) {
-
                                        tts_config_voice_s* temp_voice = (tts_config_voice_s*)calloc(1, sizeof(tts_config_voice_s));
                                        if (NULL == temp_voice) {
                                                SLOG(LOG_ERROR, tts_tag(), "[ERROR] Out of memory");
@@ -154,11 +166,17 @@ int tts_parser_get_engine_info(const char* path, tts_engine_info_s** engine_info
                                                xmlFree(attr);
                                        } else {
                                                SLOG(LOG_ERROR, tts_tag(), "[ERROR] <%s> has no content", TTS_TAG_ENGINE_VOICE_TYPE);
+                                               free(temp_voice);
+                                               temp_voice = NULL;
+                                               continue;
                                        }
 
                                        key = xmlNodeGetContent(voice_node);
                                        if (NULL != key) {
-                                               if (NULL != temp_voice->language)       free(temp_voice->language);
+                                               if (NULL != temp_voice->language) {
+                                                       free(temp_voice->language);
+                                                       temp_voice->language = NULL;
+                                               }
                                                temp_voice->language = strdup((char*)key);
                                                xmlFree(key);
                                                temp->voices = g_slist_append(temp->voices, temp_voice);
@@ -166,6 +184,7 @@ int tts_parser_get_engine_info(const char* path, tts_engine_info_s** engine_info
                                                SLOG(LOG_ERROR, tts_tag(), "[ERROR] <%s> has no content", TTS_TAG_ENGINE_VOICE);
                                                if (NULL != temp_voice) {
                                                        free(temp_voice);
+                                                       temp_voice = NULL;
                                                }
                                        }
                                }
@@ -178,6 +197,7 @@ int tts_parser_get_engine_info(const char* path, tts_engine_info_s** engine_info
                                        temp->pitch_support = true;
                                }
                                xmlFree(key);
+                               key = NULL;
                        } else {
                                SLOG(LOG_ERROR, tts_tag(), "[ERROR] <%s> has no content", TTS_TAG_ENGINE_PITCH_SUPPORT);
                        }
@@ -186,6 +206,7 @@ int tts_parser_get_engine_info(const char* path, tts_engine_info_s** engine_info
        }
 
        xmlFreeDoc(doc);
+       doc = NULL;
 
        if (NULL == temp->name || NULL == temp->uuid) {
                /* Invalid engine */
@@ -206,26 +227,42 @@ int tts_parser_free_engine_info(tts_engine_info_s* engine_info)
                return -1;
        }
 
-       if (NULL != engine_info->name)          free(engine_info->name);
-       if (NULL != engine_info->uuid)          free(engine_info->uuid);
-       if (NULL != engine_info->setting)       free(engine_info->setting);
+       if (NULL != engine_info->name) {
+               free(engine_info->name);
+               engine_info->name = NULL;
+       }
+       if (NULL != engine_info->uuid) {
+               free(engine_info->uuid);
+               engine_info->uuid = NULL;
+       }
+       if (NULL != engine_info->setting) {
+               free(engine_info->setting);
+               engine_info->setting = NULL;
+       }
 
        tts_config_voice_s *temp_voice;
        temp_voice = g_slist_nth_data(engine_info->voices, 0);
 
        while (NULL != temp_voice) {
                if (NULL != temp_voice) {
-                       if (NULL != temp_voice->language)       free(temp_voice->language);
+                       if (NULL != temp_voice->language) {
+                               free(temp_voice->language);
+                               temp_voice->language = NULL;
+                       }
                        engine_info->voices = g_slist_remove(engine_info->voices, temp_voice);
                        free(temp_voice);
+                       temp_voice = NULL;
                }
 
                temp_voice = g_slist_nth_data(engine_info->voices, 0);
        }
 
-       if (NULL != engine_info)        free(engine_info);
+       if (NULL != engine_info) {
+               free(engine_info);
+               engine_info = NULL;
+       }
 
-       return 0;       
+       return 0;
 }
 
 int tts_parser_print_engine_info(tts_engine_info_s* engine_info)
@@ -249,7 +286,7 @@ int tts_parser_print_engine_info(tts_engine_info_s* engine_info)
                /* Get a first item */
                iter = g_slist_nth(engine_info->voices, 0);
 
-               int i = 1;      
+               int i = 1;
                while (NULL != iter) {
                        /*Get handle data from list*/
                        temp_voice = iter->data;
@@ -282,6 +319,9 @@ int tts_parser_load_config(tts_config_s** config_info)
        xmlChar *key;
        bool is_default_open = false;
 
+       /* For Thread safety */
+       xmlInitParser();
+
        if (0 != access(TTS_CONFIG, F_OK)) {
                doc = xmlParseFile(TTS_DEFAULT_CONFIG);
                if (doc == NULL) {
@@ -311,12 +351,14 @@ int tts_parser_load_config(tts_config_s** config_info)
        if (cur == NULL) {
                SLOG(LOG_ERROR, tts_tag(), "[ERROR] Empty document");
                xmlFreeDoc(doc);
+               doc = NULL;
                return -1;
        }
 
        if (xmlStrcmp(cur->name, (const xmlChar *) TTS_TAG_CONFIG_BASE_TAG)) {
                SLOG(LOG_ERROR, tts_tag(), "[ERROR] The wrong type, root node is NOT %s", TTS_TAG_CONFIG_BASE_TAG);
                xmlFreeDoc(doc);
+               doc = NULL;
                return -1;
        }
 
@@ -324,6 +366,7 @@ int tts_parser_load_config(tts_config_s** config_info)
        if (cur == NULL) {
                SLOG(LOG_ERROR, tts_tag(), "[ERROR] Empty document");
                xmlFreeDoc(doc);
+               doc = NULL;
                return -1;
        }
 
@@ -344,7 +387,10 @@ int tts_parser_load_config(tts_config_s** config_info)
                if (0 == xmlStrcmp(cur->name, (const xmlChar *)TTS_TAG_CONFIG_ENGINE_ID)) {
                        key = xmlNodeGetContent(cur);
                        if (NULL != key) {
-                               if (NULL != temp->engine_id)    free(temp->engine_id);
+                               if (NULL != temp->engine_id) {
+                                       free(temp->engine_id);
+                                       temp->engine_id = NULL;
+                               }
                                temp->engine_id = strdup((char*)key);
                                xmlFree(key);
                        } else {
@@ -353,13 +399,15 @@ int tts_parser_load_config(tts_config_s** config_info)
                } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)TTS_TAG_CONFIG_ENGINE_SETTING)) {
                        key = xmlNodeGetContent(cur);
                        if (NULL != key) {
-                               if (NULL != temp->setting)      free(temp->setting);
+                               if (NULL != temp->setting) {
+                                       free(temp->setting);
+                                       temp->setting = NULL;
+                               }
                                temp->setting = strdup((char*)key);
                                xmlFree(key);
                        } else {
                                SLOG(LOG_ERROR, tts_tag(), "[ERROR] setting path is NULL");
                        }
-                       
                } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)TTS_TAG_CONFIG_AUTO_VOICE)) {
                        key = xmlNodeGetContent(cur);
                        if (NULL != key) {
@@ -397,7 +445,10 @@ int tts_parser_load_config(tts_config_s** config_info)
                } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)TTS_TAG_CONFIG_LANGUAGE)) {
                        key = xmlNodeGetContent(cur);
                        if (NULL != key) {
-                               if (NULL != temp->language)     free(temp->language);
+                               if (NULL != temp->language) {
+                                       free(temp->language);
+                                       temp->language = NULL;
+                               }
                                temp->language = strdup((char*)key);
                                xmlFree(key);
                        } else {
@@ -453,12 +504,25 @@ int tts_parser_load_config(tts_config_s** config_info)
 
 int tts_parser_unload_config(tts_config_s* config_info)
 {
-       if (NULL != g_config_doc)       xmlFreeDoc(g_config_doc);
+       if (NULL != g_config_doc) {
+               xmlFreeDoc(g_config_doc);
+               g_config_doc = NULL;
+       }
        if (NULL != config_info) {
-               if (NULL != config_info->engine_id)     free(config_info->engine_id);
-               if (NULL != config_info->setting)       free(config_info->setting);
-               if (NULL != config_info->language)      free(config_info->language);
+               if (NULL != config_info->engine_id)     {
+                       free(config_info->engine_id);
+                       config_info->engine_id = NULL;
+               }
+               if (NULL != config_info->setting) {
+                       free(config_info->setting);
+                       config_info->setting = NULL;
+               }
+               if (NULL != config_info->language) {
+                       free(config_info->language);
+                       config_info->language = NULL;
+               }
                free(config_info);
+               config_info = NULL;
        }
 
        return 0;
@@ -489,7 +553,7 @@ int tts_parser_copy_xml(const char* original, const char* destination)
                SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to change file mode : %d", ret);
        }
 
-        xmlFreeDoc(doc);
+       xmlFreeDoc(doc);
        SLOG(LOG_DEBUG, tts_tag(), "[SUCCESS] Copying xml");
 
        return 0;
@@ -767,6 +831,7 @@ int tts_parser_find_config_changed(char** engine, char**setting, bool* auto_voic
        if (cur_new == NULL || cur_old == NULL) {
                SLOG(LOG_ERROR, tts_tag(), "[ERROR] Empty document");
                xmlFreeDoc(doc);
+               doc = NULL;
                return -1;
        }
 
@@ -774,6 +839,7 @@ int tts_parser_find_config_changed(char** engine, char**setting, bool* auto_voic
        xmlStrcmp(cur_old->name, (const xmlChar*)TTS_TAG_CONFIG_BASE_TAG)) {
                SLOG(LOG_ERROR, tts_tag(), "[ERROR] The wrong type, root node is NOT %s", TTS_TAG_CONFIG_BASE_TAG);
                xmlFreeDoc(doc);
+               doc = NULL;
                return -1;
        }
 
@@ -782,6 +848,7 @@ int tts_parser_find_config_changed(char** engine, char**setting, bool* auto_voic
        if (cur_new == NULL || cur_old == NULL) {
                SLOG(LOG_ERROR, tts_tag(), "[ERROR] Empty document");
                xmlFreeDoc(doc);
+               doc = NULL;
                return -1;
        }
 
@@ -795,7 +862,10 @@ int tts_parser_find_config_changed(char** engine, char**setting, bool* auto_voic
                                                if (0 != xmlStrcmp(key_old, key_new)) {
                                                        SLOG(LOG_DEBUG, tts_tag(), "Old engine id(%s), New engine(%s)", 
                                                                (char*)key_old, (char*)key_new);
-                                                       if (NULL != *engine)    free(*engine);
+                                                       if (NULL != *engine) {
+                                                               free(*engine);
+                                                               *engine = NULL;
+                                                       }
                                                        *engine = strdup((char*)key_new);
                                                }
                                                xmlFree(key_new);
@@ -814,7 +884,10 @@ int tts_parser_find_config_changed(char** engine, char**setting, bool* auto_voic
                                                if (0 != xmlStrcmp(key_old, key_new)) {
                                                        SLOG(LOG_DEBUG, tts_tag(), "Old engine setting(%s), New engine setting(%s)", 
                                                                (char*)key_old, (char*)key_new);
-                                                       if (NULL != *setting)   free(*setting);
+                                                       if (NULL != *setting) {
+                                                               free(*setting);
+                                                               *setting = NULL;
+                                                       }
                                                        *setting = strdup((char*)key_new);
                                                }
                                                xmlFree(key_new);
@@ -855,7 +928,10 @@ int tts_parser_find_config_changed(char** engine, char**setting, bool* auto_voic
                                                if (0 != xmlStrcmp(key_old, key_new)) {
                                                        SLOG(LOG_DEBUG, tts_tag(), "Old language(%s), New language(%s)", 
                                                                (char*)key_old, (char*)key_new);
-                                                       if (NULL != *language)  free(*language);
+                                                       if (NULL != *language) {
+                                                               free(*language);
+                                                               *language = NULL;
+                                                       }
                                                        *language = strdup((char*)key_new);
                                                }
                                                xmlFree(key_new);
@@ -934,8 +1010,11 @@ int tts_parser_find_config_changed(char** engine, char**setting, bool* auto_voic
                cur_new = cur_new->next;
                cur_old = cur_old->next;
        }
-       
-       xmlFreeDoc(g_config_doc);
+
+       if (NULL != g_config_doc) {
+               xmlFreeDoc(g_config_doc);
+               g_config_doc = NULL;
+       }
        g_config_doc = doc;
 
        return 0;