From f88c61b3dd2608243f2e84f7c6ae390a9017ed86 Mon Sep 17 00:00:00 2001 From: Dewal Agarwal Date: Wed, 21 Sep 2022 19:58:59 +0530 Subject: [PATCH] Fix: Reorder variable declaration to fix memory leaks -- xml objects are not freed. -- free them using xmlFree, change scope of variables. Change-Id: If465aee96312786d991ea5fc66c4b0c5eddb510d Signed-off-by: Dewal Agarwal --- packaging/account-parser.spec | 2 +- src/account.c | 109 +++++++++++++++++++++++++++++++++++++----- 2 files changed, 99 insertions(+), 12 deletions(-) diff --git a/packaging/account-parser.spec b/packaging/account-parser.spec index 8ffbd29..670c317 100644 --- a/packaging/account-parser.spec +++ b/packaging/account-parser.spec @@ -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 diff --git a/src/account.c b/src/account.c index 0c5f734..da8105a 100644 --- a/src/account.c +++ b/src/account.c @@ -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: 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: 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: 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: