[libgepub] Transforming xmlChar* to gchar* and freeing correctly



commit 6c92f66224e83c37f259b3082c6a39d23d30df60
Author: Daniel GarcĂ­a Moreno <danigm wadobo com>
Date:   Thu Jun 22 09:37:20 2017 +0200

    Transforming xmlChar* to gchar* and freeing correctly
    
    https://bugzilla.gnome.org/show_bug.cgi?id=784049

 libgepub/gepub-archive.c |    2 +-
 libgepub/gepub-doc.c     |   14 +++++---------
 libgepub/gepub-utils.c   |   21 +++++++++++++++++++++
 libgepub/gepub-utils.h   |    1 +
 4 files changed, 28 insertions(+), 10 deletions(-)
---
diff --git a/libgepub/gepub-archive.c b/libgepub/gepub-archive.c
index 0eb11ac..ba5bb15 100644
--- a/libgepub/gepub-archive.c
+++ b/libgepub/gepub-archive.c
@@ -174,7 +174,7 @@ gepub_archive_get_root_file (GepubArchive *archive)
     doc = xmlRecoverMemory (buffer, bufsize);
     root_element = xmlDocGetRootElement (doc);
     root_node = gepub_utils_get_element_by_tag (root_element, "rootfile");
-    root_file = xmlGetProp (root_node, "full-path");
+    root_file = gepub_utils_get_prop (root_node, "full-path");
 
     xmlFreeDoc (doc);
     g_bytes_unref (bytes);
diff --git a/libgepub/gepub-doc.c b/libgepub/gepub-doc.c
index 6d50785..0c9d48e 100644
--- a/libgepub/gepub-doc.c
+++ b/libgepub/gepub-doc.c
@@ -250,13 +250,13 @@ gepub_doc_fill_resources (GepubDoc *doc)
             continue;
         }
 
-        id = xmlGetProp (item, "id");
-        tmpuri = xmlGetProp (item, "href");
+        id = gepub_utils_get_prop (item, "id");
+        tmpuri = gepub_utils_get_prop (item, "href");
         uri = g_strdup_printf ("%s%s", doc->content_base, tmpuri);
         g_free (tmpuri);
 
         res = g_malloc (sizeof (GepubResource));
-        res->mime = xmlGetProp (item, "media-type");
+        res->mime = gepub_utils_get_prop (item, "media-type");
         res->uri = uri;
         g_hash_table_insert (doc->resources, id, res);
         item = item->next;
@@ -289,7 +289,7 @@ gepub_doc_fill_spine (GepubDoc *doc)
             continue;
         }
 
-        id = xmlGetProp (item, "idref");
+        id = gepub_utils_get_prop (item, "idref");
 
         spine = g_list_prepend (spine, id);
         item = item->next;
@@ -699,7 +699,6 @@ gepub_doc_get_cover (GepubDoc *doc)
     xmlNode *root_element = NULL;
     xmlNode *mnode = NULL;
     gchar *ret;
-    xmlChar *text;
     const char *data;
     gsize size;
 
@@ -710,10 +709,7 @@ gepub_doc_get_cover (GepubDoc *doc)
     xdoc = xmlRecoverMemory (data, size);
     root_element = xmlDocGetRootElement (xdoc);
     mnode = gepub_utils_get_element_by_attr (root_element, "name", "cover");
-    text = xmlGetProp(mnode, "content");
-
-    ret = g_strdup (text);
-    xmlFree (text);
+    ret = gepub_utils_get_prop (mnode, "content");
 
     xmlFreeDoc (xdoc);
 
diff --git a/libgepub/gepub-utils.c b/libgepub/gepub-utils.c
index 1484b6b..aa1ba41 100644
--- a/libgepub/gepub-utils.c
+++ b/libgepub/gepub-utils.c
@@ -154,6 +154,7 @@ gepub_utils_get_element_by_attr (xmlNode *node, const gchar *attr, const gchar *
         if (cur_node->type == XML_ELEMENT_NODE ) {
             text = xmlGetProp (cur_node, attr);
             if (text && !strcmp (text, value)) {
+                xmlFree (text);
                 return cur_node;
             }
             if (text) {
@@ -256,3 +257,23 @@ gepub_utils_replace_resources (GBytes *content, const gchar *path)
 
     return g_bytes_new_take (buffer, bufsize);
 }
+
+
+/**
+ * Returns a gchar* with the property with the name prop in the xmlNode
+ * node
+ */
+gchar *
+gepub_utils_get_prop (xmlNode *node, const gchar *prop)
+{
+    xmlChar *p = NULL;
+    gchar *ret = NULL;
+
+    p = xmlGetProp (node, (const xmlChar *) prop);
+    if (p) {
+        ret = g_strdup ((char *) p);
+        xmlFree (p);
+    }
+
+    return ret;
+}
diff --git a/libgepub/gepub-utils.h b/libgepub/gepub-utils.h
index 1dbf388..267301a 100644
--- a/libgepub/gepub-utils.h
+++ b/libgepub/gepub-utils.h
@@ -26,5 +26,6 @@ xmlNode * gepub_utils_get_element_by_tag  (xmlNode *node, const gchar *name);
 xmlNode * gepub_utils_get_element_by_attr (xmlNode *node, const gchar *attr, const gchar *value);
 GList *   gepub_utils_get_text_elements   (xmlNode *node);
 GBytes *  gepub_utils_replace_resources   (GBytes *content, const gchar *path);
+gchar *   gepub_utils_get_prop            (xmlNode *node, const gchar *prop);
 
 #endif


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]