From 07cb1b8d55452aea59048263d7d0f2176fa8262c Mon Sep 17 00:00:00 2001 From: Ben Kibbey Date: Tue, 25 Dec 2007 15:53:21 -0500 Subject: [PATCH] Use xmlDocGetRootElement() and xmlNodeGetContent() in list_accounts() to prevent some errors. --- src/xml.c | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/src/xml.c b/src/xml.c index a41f2cf7..040341c4 100644 --- a/src/xml.c +++ b/src/xml.c @@ -160,26 +160,20 @@ xmlChar *new_document() */ gpg_error_t list_accounts(xmlDocPtr doc, GString **result) { - xmlNodePtr n = NULL, t; + xmlNodePtr n = NULL; GSList *list = NULL; gint total, i; GString *string; + gpg_error_t error = 0; - for (t = doc->children; t; t = t->next) { - if (t->type == XML_ELEMENT_NODE) { - if (xmlStrEqual(t->name, (xmlChar *)"accounts")) { - n = t; - break; - } - } - } + n = xmlDocGetRootElement(doc); if (!n || !n->children) return EPWMD_EMPTY_ELEMENT; for (n = n->children; n; n = n->next) { xmlAttrPtr a; - gchar *tmp; + xmlChar *val; GSList *tlist; if (n->type != XML_ELEMENT_NODE) @@ -190,18 +184,18 @@ gpg_error_t list_accounts(xmlDocPtr doc, GString **result) if (!a || !a->children->content) continue; - tmp = g_strdup((gchar *)a->children->content); + val = xmlNodeGetContent(a->children); - if (!tmp) { - g_slist_free(list); - return gpg_error_from_errno(ENOMEM); + if (!val) { + error = gpg_error_from_errno(ENOMEM); + goto fail; } - tlist = g_slist_append(list, tmp); + tlist = g_slist_append(list, (gchar *)val); if (!tlist) { - g_slist_free(list); - return gpg_error_from_errno(ENOMEM); + error = gpg_error_from_errno(ENOMEM); + goto fail; } list = tlist; @@ -215,21 +209,27 @@ gpg_error_t list_accounts(xmlDocPtr doc, GString **result) string = g_string_new(NULL); if (!string) { - g_slist_free(list); - return gpg_error_from_errno(ENOMEM); + error = gpg_error_from_errno(ENOMEM); + goto fail; } for (i = 0; i < total; i++) { - gchar *tmp = g_slist_nth_data(list, i); + xmlChar *val = g_slist_nth_data(list, i); - g_string_append_printf(string, "%s\n", tmp); - g_free(tmp); + g_string_append_printf(string, "%s\n", val); } string = g_string_truncate(string, string->len - 1); - g_slist_free(list); *result = string; - return 0; + +fail: + total = g_slist_length(list); + + for (i = 0; i < total; i++) + xmlFree(g_slist_nth_data(list, i)); + + g_slist_free(list); + return error; } gchar **split_input_line(gchar *str, gchar *delim, gint n) -- 2.11.4.GIT