Fix: Reorder variable declaration to fix memory leaks 65/281765/3 accepted/tizen_7.0_unified accepted/tizen_7.0_unified_hotfix accepted/tizen_8.0_unified accepted/tizen_unified tizen tizen_7.0 tizen_7.0_hotfix tizen_8.0 accepted/tizen/7.0/unified/20221110.061854 accepted/tizen/7.0/unified/hotfix/20221116.104538 accepted/tizen/8.0/unified/20231005.092553 accepted/tizen/unified/20220925.234919 accepted/tizen/unified/20220925.234948 accepted/tizen/unified/20220926.025512 accepted/tizen/unified/20220928.020801 tizen_7.0_m2_release tizen_8.0_m2_release
authorDewal Agarwal <d1.agarwal@samsung.com>
Wed, 21 Sep 2022 14:28:59 +0000 (19:58 +0530)
committerDewal Agarwal <d1.agarwal@samsung.com>
Thu, 22 Sep 2022 16:10:39 +0000 (21:40 +0530)
-- xml objects are not freed.
-- free them using xmlFree, change scope of variables.

Change-Id: If465aee96312786d991ea5fc66c4b0c5eddb510d
Signed-off-by: Dewal Agarwal <d1.agarwal@samsung.com>
packaging/account-parser.spec
src/account.c

index 8ffbd29..670c317 100644 (file)
@@ -1,6 +1,6 @@
 Name:          account-parser
 Summary:       Account Parser Library
-Version:       0.1.5
+Version:       0.1.6
 Release:       0
 Group:         Social & Content/Other
 License:       Apache-2.0
index 0c5f734..da8105a 100644 (file)
@@ -134,6 +134,16 @@ int _register_account_provider(xmlDocPtr docPtr, char* account_provider_app_id)
                return ret;
        }
 
+       xmlChar* attribute_app_id = NULL;
+       xmlChar* attribute_provider_id = NULL;
+       xmlChar* multiple_accounts_support = NULL;
+       xmlChar* section = NULL;
+       xmlChar* account_icon = NULL;
+       xmlChar* account_small_icon = NULL;
+       xmlChar* xml_lang = NULL;
+       xmlChar* xml_label = NULL;
+       xmlChar* xml_capability = NULL;
+
        // Node: <account>
        xmlNodePtr cur_ptr = xmlFirstElementChild(xmlDocGetRootElement(docPtr));
        if (cur_ptr == NULL) {
@@ -158,7 +168,7 @@ int _register_account_provider(xmlDocPtr docPtr, char* account_provider_app_id)
                // Node: <account-provider>
                if ((!xmlStrcmp(cur_ptr->name, _NODE_ACCOUNT_PROVIDER))) {
                        // Attribute: appid
-                       xmlChar* attribute_app_id = xmlGetProp(cur_ptr, _ATTRIBUTE_APP_ID);
+                       attribute_app_id = xmlGetProp(cur_ptr, _ATTRIBUTE_APP_ID);
                        if (attribute_app_id == NULL) {
                                ret = -1;
                                _E("Failed to get the attribute.");
@@ -174,7 +184,7 @@ int _register_account_provider(xmlDocPtr docPtr, char* account_provider_app_id)
                        }
 
                        // Attribute: providerid
-                       xmlChar* attribute_provider_id = xmlGetProp(cur_ptr, _ATTRIBUTE_SERVICE_PROVIDER_ID);
+                       attribute_provider_id = xmlGetProp(cur_ptr, _ATTRIBUTE_SERVICE_PROVIDER_ID);
                        if (attribute_provider_id != NULL) {
                                ret = -1;
                                _E("Failed to get the attribute(providerid).");
@@ -187,7 +197,7 @@ int _register_account_provider(xmlDocPtr docPtr, char* account_provider_app_id)
                        }
 
                        // Attribute: multiple-accounts-support
-                       xmlChar* multiple_accounts_support = xmlGetProp(cur_ptr, _ATTRIBUTE_MULTIPLE_ACCOUNTS_SUPPORT);
+                       multiple_accounts_support = xmlGetProp(cur_ptr, _ATTRIBUTE_MULTIPLE_ACCOUNTS_SUPPORT);
                        if (multiple_accounts_support == NULL) {
                                ret = -1;
                                _E("Failed to get the attribute.");
@@ -224,7 +234,7 @@ int _register_account_provider(xmlDocPtr docPtr, char* account_provider_app_id)
                                // Node: <icon>
                                if ((!xmlStrcmp(cur_ptr->name, _NODE_ICON))) {
                                        // Attribute: section
-                                       xmlChar* section = xmlGetProp(cur_ptr, _ATTRIBUTE_SECTION);
+                                       section = xmlGetProp(cur_ptr, _ATTRIBUTE_SECTION);
                                        if (section == NULL) {
                                                ret = -1;
                                                _E("Failed to get the attribute.");
@@ -235,7 +245,7 @@ int _register_account_provider(xmlDocPtr docPtr, char* account_provider_app_id)
 
                                        char *resource_path = NULL;
                                        if ((!xmlStrcmp(section, _VALUE_ACCOUNT))) {
-                                               xmlChar* account_icon = xmlNodeListGetString(docPtr, cur_ptr->xmlChildrenNode, 1);
+                                               account_icon = xmlNodeListGetString(docPtr, cur_ptr->xmlChildrenNode, 1);
                                                if (account_icon == NULL) {
                                                        ret = -1;
                                                        _E("Failed to get the value.");
@@ -297,8 +307,12 @@ int _register_account_provider(xmlDocPtr docPtr, char* account_provider_app_id)
                                                                g_free(icon_path);
                                                        }
                                                }
+                                               if (account_icon != NULL) {
+                                                       xmlFree(account_icon);
+                                                       account_icon = NULL;
+                                               }
                                        } else if ((!xmlStrcmp(section, _VALUE_ACCOUNT_SMALL))) {
-                                               xmlChar* account_small_icon = xmlNodeListGetString(docPtr, cur_ptr->xmlChildrenNode, 1);
+                                               account_small_icon = xmlNodeListGetString(docPtr, cur_ptr->xmlChildrenNode, 1);
                                                if (account_small_icon == NULL) {
                                                        ret = -1;
                                                        _E("Failed to get the value.");
@@ -362,6 +376,15 @@ int _register_account_provider(xmlDocPtr docPtr, char* account_provider_app_id)
                                                                g_free(small_icon_path);
                                                        }
                                                }
+                                               if (account_small_icon != NULL) {
+                                                       xmlFree(account_small_icon);
+                                                       account_small_icon = NULL;
+                                               }
+                                       }
+
+                                       if (section != NULL) {
+                                               xmlFree(section);
+                                               section = NULL;
                                        }
                                } else if ((!xmlStrcmp(cur_ptr->name, _NODE_LABEL))) {
                                        // Node: <label>
@@ -369,7 +392,7 @@ int _register_account_provider(xmlDocPtr docPtr, char* account_provider_app_id)
                                        _SECURE_D("Node: %s", cur_ptr->name);
 
                                        // Attribute: xml:lang
-                                       xmlChar* xml_lang = xmlNodeGetLang(cur_ptr);
+                                       xml_lang = xmlNodeGetLang(cur_ptr);
                                        if (xml_lang != NULL) {
                                                _SECURE_D("Attribute: xml:lang - %s", xml_lang);
 
@@ -419,7 +442,7 @@ int _register_account_provider(xmlDocPtr docPtr, char* account_provider_app_id)
 
                                                g_strfreev(tokens);
 
-                                               xmlChar* xml_label = xmlNodeListGetString(docPtr, cur_ptr->xmlChildrenNode, 1);
+                                               xml_label = xmlNodeListGetString(docPtr, cur_ptr->xmlChildrenNode, 1);
                                                if (xml_label == NULL) {
                                                        ret = -1;
                                                        g_free(converted_lang);
@@ -438,7 +461,7 @@ int _register_account_provider(xmlDocPtr docPtr, char* account_provider_app_id)
 
                                                g_free(converted_lang);
                                        } else {
-                                               xmlChar* xml_label = xmlNodeListGetString(docPtr, cur_ptr->xmlChildrenNode, 1);
+                                               xml_label = xmlNodeListGetString(docPtr, cur_ptr->xmlChildrenNode, 1);
                                                if (xml_label == NULL) {
                                                        ret = -1;
                                                        _E("Failed to get the value.");
@@ -453,12 +476,22 @@ int _register_account_provider(xmlDocPtr docPtr, char* account_provider_app_id)
                                                        goto CATCH;
                                                }
                                        }
+
+                                       if (xml_lang != NULL) {
+                                               xmlFree(xml_lang);
+                                               xml_lang = NULL;
+                                       }
+                                       if (xml_label != NULL) {
+                                               xmlFree(xml_label);
+                                               xml_label = NULL;
+                                       }
+
                                } else if ((!xmlStrcmp(cur_ptr->name, _NODE_CAPABILITY))) {
                                        // Node: <capability>
 
                                        _SECURE_D("Node: %s", cur_ptr->name);
 
-                                       xmlChar* xml_capability = xmlNodeListGetString(docPtr, cur_ptr->xmlChildrenNode, 1);
+                                       xml_capability = xmlNodeListGetString(docPtr, cur_ptr->xmlChildrenNode, 1);
                                        if (xml_capability == NULL) {
                                                ret = -1;
                                                _E("Failed to get the value.");
@@ -472,11 +505,28 @@ int _register_account_provider(xmlDocPtr docPtr, char* account_provider_app_id)
                                                _E("[%d]Failed to set the capability.", ret);
                                                goto CATCH;
                                        }
-                               }
 
+                                       if (xml_capability != NULL) {
+                                               xmlFree(xml_capability);
+                                               xml_capability = NULL;
+                                       }
+                               }
                                cur_ptr = cur_ptr->next;
                        }
 
+                       if (attribute_app_id != NULL) {
+                               xmlFree(attribute_app_id);
+                               attribute_app_id = NULL;
+                       }
+                       if (attribute_provider_id != NULL) {
+                               xmlFree(attribute_provider_id);
+                               attribute_provider_id = NULL;
+                       }
+                       if (multiple_accounts_support != NULL) {
+                               xmlFree(multiple_accounts_support);
+                               multiple_accounts_support = NULL;
+                       }
+
                        break;
                }
 
@@ -512,6 +562,43 @@ int _register_account_provider(xmlDocPtr docPtr, char* account_provider_app_id)
        return 0;
 
 CATCH:
+       if (attribute_app_id != NULL) {
+               xmlFree(attribute_app_id);
+               attribute_app_id = NULL;
+       }
+       if (attribute_provider_id != NULL) {
+               xmlFree(attribute_provider_id);
+               attribute_provider_id = NULL;
+       }
+       if (multiple_accounts_support != NULL) {
+               xmlFree(multiple_accounts_support);
+               multiple_accounts_support = NULL;
+       }
+       if (section != NULL) {
+               xmlFree(section);
+               section = NULL;
+       }
+       if (account_icon != NULL) {
+               xmlFree(account_icon);
+               account_icon = NULL;
+       }
+       if (account_small_icon != NULL) {
+               xmlFree(account_small_icon);
+               account_small_icon = NULL;
+       }
+       if (xml_lang != NULL) {
+               xmlFree(xml_lang);
+               xml_lang = NULL;
+       }
+       if (xml_label != NULL) {
+               xmlFree(xml_label);
+               xml_label = NULL;
+       }
+       if (xml_capability != NULL) {
+               xmlFree(xml_capability);
+               xml_capability = NULL;
+       }
+
        ret2 = account_type_destroy(account_type_handle);
        retvm_if(ret2 != ACCOUNT_ERROR_NONE, ret2, "[%d]Failed to perform account_type_destroy().", ret2);