Fix static analysis issue
[platform/core/appfw/pkgmgr-info.git] / parser / src / pkgmgr_parser_deprecated.c
index 522fc83..d717409 100644 (file)
@@ -75,8 +75,13 @@ static void __save_xml_attribute(xmlTextReaderPtr reader, char *attribute, char
 
 static void __save_xml_value(xmlTextReaderPtr reader, char **xml_attribute)
 {
-       xmlTextReaderRead(reader);
-       const xmlChar *attrib_val = xmlTextReaderConstValue(reader);
+       int ret;
+       const xmlChar *attrib_val;
+
+       ret = xmlTextReaderRead(reader);
+       if (ret == -1)
+               return;
+       attrib_val = xmlTextReaderConstValue(reader);
 
        if (attrib_val)
                *xml_attribute = strdup((const char *)attrib_val);
@@ -323,13 +328,18 @@ static char *__get_icon_with_path(const char *icon, uid_t uid)
 
 static int __ps_process_icon(xmlTextReaderPtr reader, icon_x *icon, uid_t uid)
 {
+       int ret;
+       char *text;
+
        __save_xml_attribute(reader, "section", &icon->section, NULL);
        __save_xml_attribute(reader, "size", &icon->size, NULL);
        __save_xml_attribute(reader, "resolution", &icon->resolution, NULL);
        __save_xml_lang(reader, &icon->lang);
 
-       xmlTextReaderRead(reader);
-       char *text  = ASCII(xmlTextReaderValue(reader));
+       ret = xmlTextReaderRead(reader);
+       if (ret == -1)
+               return 0;
+       text  = ASCII(xmlTextReaderValue(reader));
        if (text) {
                icon->text = __get_icon_with_path(text, uid);
                free(text);
@@ -392,6 +402,10 @@ static void __ps_process_mime(gpointer data, gpointer user_data)
        snprintf(ad->mime, sizeof(ad->mime), "%s", mime);
 
        appcontrol = calloc(1, sizeof(appcontrol_x));
+       if (appcontrol == NULL) {
+               _LOGD("Malloc Failed\n");
+               return;
+       }
        if (strlen(ad->operation))
                appcontrol->operation = strdup(ad->operation);
        if (strlen(ad->uri))
@@ -412,6 +426,10 @@ static void __ps_process_uri(gpointer data, gpointer user_data)
                g_list_foreach(ad->mimes, __ps_process_mime, user_data);
        } else {
                appcontrol = calloc(1, sizeof(appcontrol_x));
+               if (appcontrol == NULL) {
+                       _LOGD("Malloc Failed\n");
+                       return;
+               }
                if (strlen(ad->operation))
                        appcontrol->operation = strdup(ad->operation);
                appcontrol->uri = strdup(ad->uri);
@@ -433,6 +451,10 @@ static void __ps_process_operation(gpointer data, gpointer user_data)
                g_list_foreach(ad->mimes, __ps_process_mime, user_data);
        } else {
                appcontrol = calloc(1, sizeof(appcontrol_x));
+               if (appcontrol == NULL) {
+                       _LOGD("Malloc Failed\n");
+                       return;
+               }
                appcontrol->operation = strdup(ad->operation);
                ad->appcontrols = g_list_append(ad->appcontrols, appcontrol);
        }
@@ -470,6 +492,9 @@ static int __ps_process_appcontrol(xmlTextReaderPtr reader, GList **appcontrol)
                node = xmlTextReaderConstName(reader);
                if (!node) {
                        _LOGD("xmlTextReaderConstName value is NULL\n");
+                       g_list_free_full(operations, free);
+                       g_list_free_full(uris, free);
+                       g_list_free_full(mimes, free);
                        return -1;
                }
 
@@ -1095,31 +1120,31 @@ static void __ps_process_tag(manifest_x * mfx, char *const tagv[])
 
        for (tag = strdup(tagv[0]); tag != NULL; ) {
                ret_result = strtok_r(tag, delims, &ptr);
-
-               /*check tag :  preload */
-               if (strcmp(ret_result, "preload") == 0) {
-                       ret_result = strtok_r(NULL, delims, &ptr);
-                       if (strcmp(ret_result, "true") == 0) {
-                               free((void *)mfx->preload);
-                               mfx->preload = strdup("true");
-                       } else if (strcmp(ret_result, "false") == 0) {
-                               free((void *)mfx->preload);
-                               mfx->preload = strdup("false");
-                       }
-               /*check tag :  removable*/
-               } else if (strcmp(ret_result, "removable") == 0) {
-                       ret_result = strtok_r(NULL, delims, &ptr);
-                       if (strcmp(ret_result, "true") == 0) {
-                               free((void *)mfx->removable);
-                               mfx->removable = strdup("true");
-                       } else if (strcmp(ret_result, "false") == 0) {
-                               free((void *)mfx->removable);
-                               mfx->removable = strdup("false");
-                       }
-               /*check tag :  not matched*/
-               } else
-                       _LOGD("tag process [%s]is not defined\n", ret_result);
-
+               if (ret_result != NULL) {
+                       /*check tag :  preload */
+                       if (strcmp(ret_result, "preload") == 0) {
+                               ret_result = strtok_r(NULL, delims, &ptr);
+                               if (ret_result && strcmp(ret_result, "true") == 0) {
+                                       free((void *)mfx->preload);
+                                       mfx->preload = strdup("true");
+                               } else if (ret_result && strcmp(ret_result, "false") == 0) {
+                                       free((void *)mfx->preload);
+                                       mfx->preload = strdup("false");
+                               }
+                       /*check tag :  removable*/
+                       } else if (strcmp(ret_result, "removable") == 0) {
+                               ret_result = strtok_r(NULL, delims, &ptr);
+                               if (ret_result && strcmp(ret_result, "true") == 0) {
+                                       free((void *)mfx->removable);
+                                       mfx->removable = strdup("true");
+                               } else if (ret_result && strcmp(ret_result, "false") == 0) {
+                                       free((void *)mfx->removable);
+                                       mfx->removable = strdup("false");
+                               }
+                       /*check tag :  not matched*/
+                       } else
+                               _LOGD("tag process [%s]is not defined\n", ret_result);
+               }
                free(tag);
 
                /*check next value*/
@@ -1346,7 +1371,10 @@ static void *__open_lib_handle(char *tag)
        retvm_if(!lib_path, NULL, "lib_path get fail");
 
        lib_handle = dlopen(lib_path, RTLD_LAZY);
-       retvm_if(lib_handle == NULL, NULL, "dlopen is failed lib_path[%s]", lib_path);
+       if (lib_handle == NULL)
+               _LOGE("dlopen is failed lib_path[%s]", lib_path);
+
+       free(lib_path);
 
        return lib_handle;
 }
@@ -1662,12 +1690,13 @@ static int __run_metadata_parser_prestep(manifest_x *mfx, char *md_key, ACTION_T
                tag_exist = 0;
        }
 
+       free(md_tag);
+
        return 0;
 END:
        __metadata_parser_clear_dir_list(md_list);
 
-       if (md_tag)
-               free(md_tag);
+       free(md_tag);
 
        return ret;
 }
@@ -1880,12 +1909,13 @@ static int __run_category_parser_prestep(manifest_x *mfx, char *category_key, AC
                tag_exist = 0;
        }
 
+       free(category_tag);
+
        return 0;
 END:
        __category_parser_clear_dir_list(category_list);
 
-       if (category_tag)
-               free(category_tag);
+       free(category_tag);
 
        return ret;
 }
@@ -1932,7 +1962,11 @@ DEPRECATED API int pkgmgr_parser_parse_manifest_for_installation(const char *man
        __ps_process_tag(mfx, tagv);
 
        ret = pkgmgr_parser_insert_manifest_info_in_db(mfx);
-       retvm_if(ret == PMINFO_R_ERROR, PMINFO_R_ERROR, "DB Insert failed");
+       if (ret == PMINFO_R_ERROR) {
+               _LOGE("DB Insert failed");
+               pkgmgr_parser_free_manifest_xml(mfx);
+               return PMINFO_R_ERROR;
+       }
 
        _LOGD("DB Insert Success\n");
 
@@ -1968,7 +2002,11 @@ DEPRECATED API int pkgmgr_parser_parse_usr_manifest_for_installation(const char
        __ps_process_tag(mfx, tagv);
 
        ret = pkgmgr_parser_insert_manifest_info_in_usr_db(mfx, uid);
-       retvm_if(ret == PMINFO_R_ERROR, PMINFO_R_ERROR, "DB Insert failed");
+       if (ret == PMINFO_R_ERROR) {
+               _LOGE("DB Insert failed");
+               pkgmgr_parser_free_manifest_xml(mfx);
+               return PMINFO_R_ERROR;
+       }
 
        _LOGD("DB Insert Success\n");
 
@@ -2050,13 +2088,18 @@ DEPRECATED API int pkgmgr_parser_parse_manifest_for_upgrade(const char *manifest
 
        /*Delete from cert table*/
        ret = pkgmgrinfo_delete_certinfo(mfx->package);
-       if (ret) {
+       if (ret != PMINFO_R_OK) {
                _LOGD("Cert Info  DB Delete Failed\n");
-               return -1;
+               pkgmgr_parser_free_manifest_xml(mfx);
+               return PMINFO_R_ERROR;
        }
 
        ret = pkgmgr_parser_update_manifest_info_in_db(mfx);
-       retvm_if(ret == PMINFO_R_ERROR, PMINFO_R_ERROR, "DB Insert failed");
+       if (ret != PMINFO_R_OK) {
+               _LOGD("DB Insert failed\n");
+               pkgmgr_parser_free_manifest_xml(mfx);
+               return PMINFO_R_ERROR;
+       }
 
        _LOGD("DB Update Success\n");
 
@@ -2125,13 +2168,19 @@ DEPRECATED API int pkgmgr_parser_parse_usr_manifest_for_upgrade(const char *mani
 
        /*Delete from cert table*/
        ret = pkgmgrinfo_delete_certinfo(mfx->package);
-       if (ret) {
+       if (ret != PMINFO_R_OK) {
                _LOGD("Cert Info  DB Delete Failed\n");
-               return -1;
+               pkgmgr_parser_free_manifest_xml(mfx);
+               return PMINFO_R_ERROR;
        }
 
        ret = pkgmgr_parser_update_manifest_info_in_usr_db(mfx, uid);
-       retvm_if(ret == PMINFO_R_ERROR, PMINFO_R_ERROR, "DB Insert failed");
+       if (ret != PMINFO_R_OK) {
+               _LOGD("DB Insert failed\n");
+               pkgmgr_parser_free_manifest_xml(mfx);
+               return PMINFO_R_ERROR;
+       }
+
        _LOGD("DB Update Success\n");
 
        __ps_process_tag_parser(mfx, manifest, ACTION_UPGRADE);
@@ -2176,6 +2225,7 @@ API int pkgmgr_parser_parse_manifest_for_uninstallation(const char *manifest, ch
        ret = pkgmgrinfo_delete_certinfo(mfx->package);
        if (ret) {
                _LOGD("Cert Info  DB Delete Failed\n");
+               pkgmgr_parser_free_manifest_xml(mfx);
                return -1;
        }
 
@@ -2256,6 +2306,7 @@ API int pkgmgr_parser_parse_usr_manifest_for_uninstallation(const char *manifest
        ret = pkgmgrinfo_delete_certinfo(mfx->package);
        if (ret) {
                _LOGD("Cert Info  DB Delete Failed\n");
+               pkgmgr_parser_free_manifest_xml(mfx);
                return -1;
        }