c400dd
From a09ea6bd74d6234be8456e7039403bc1c1d078bd Mon Sep 17 00:00:00 2001
c400dd
From: Christophe Fergeau <cfergeau@redhat.com>
c400dd
Date: Mon, 20 Jun 2016 12:05:48 +0200
c400dd
Subject: [PATCH 1/4] xml-node: Use GString in rest_xml_node_print()
c400dd
c400dd
The current code is using xml = g_strconcat (xml, ...) which is causing
c400dd
some leaks as g_strconcat returns a newly allocated string. Using
c400dd
GString avoids this issue without constantly freeing the intermediate
c400dd
strings.
c400dd
c400dd
This fixes multiple leaks like:
c400dd
c400dd
==16611== 18 bytes in 1 blocks are definitely lost in loss record 124 of 301
c400dd
==16611==    at 0x4C2BBAD: malloc (vg_replace_malloc.c:299)
c400dd
==16611==    by 0x5F5CE58: g_malloc (gmem.c:94)
c400dd
==16611==    by 0x5F75B8E: g_strconcat (gstrfuncs.c:585)
c400dd
==16611==    by 0x4E450CF: rest_xml_node_print (rest-xml-node.c:287)
c400dd
==16611==    by 0x4E451DA: rest_xml_node_print (rest-xml-node.c:305)
c400dd
==16611==    by 0x4E450F8: rest_xml_node_print (rest-xml-node.c:292)
c400dd
==16611==    by 0x4009A0: main (xml.c:40)
c400dd
---
c400dd
 rest/rest-xml-node.c | 21 ++++++++++++---------
c400dd
 1 file changed, 12 insertions(+), 9 deletions(-)
c400dd
c400dd
diff --git a/rest/rest-xml-node.c b/rest/rest-xml-node.c
c400dd
index 57a942667f06..a8156dbbd432 100644
c400dd
--- a/rest/rest-xml-node.c
c400dd
+++ b/rest/rest-xml-node.c
c400dd
@@ -283,38 +283,41 @@ rest_xml_node_print (RestXmlNode *node)
c400dd
 {
c400dd
   GHashTableIter iter;
c400dd
   gpointer       key, value;
c400dd
-  char          *xml = g_strconcat ("<", node->name, NULL);
c400dd
+  GString        *xml = g_string_new (NULL);
c400dd
   RestXmlNode   *n;
c400dd
 
c400dd
+  g_string_append (xml, "<");
c400dd
+  g_string_append (xml, node->name);
c400dd
+
c400dd
   g_hash_table_iter_init (&iter, node->attrs);
c400dd
   while (g_hash_table_iter_next (&iter, &key, &value))
c400dd
-    xml = g_strconcat (xml, " ", key, "=\'", value, "\'", NULL);
c400dd
+    g_string_append_printf (xml, " %s =\'%s\'", (char *)key, (char *)value);
c400dd
 
c400dd
-  xml = g_strconcat (xml, ">", NULL);
c400dd
+  g_string_append (xml, ">");
c400dd
 
c400dd
   g_hash_table_iter_init (&iter, node->children);
c400dd
   while (g_hash_table_iter_next (&iter, &key, &value))
c400dd
     {
c400dd
       char *child = rest_xml_node_print ((RestXmlNode *) value);
c400dd
 
c400dd
-      xml = g_strconcat (xml, child, NULL);
c400dd
+      g_string_append (xml, child);
c400dd
       g_free (child);
c400dd
     }
c400dd
 
c400dd
   if (node->content)
c400dd
-    xml = g_strconcat (xml, node->content, "</", node->name, ">", NULL);
c400dd
-  else
c400dd
-    xml = g_strconcat (xml, "</", node->name, ">", NULL);
c400dd
+    g_string_append (xml, node->content);
c400dd
+
c400dd
+  g_string_append_printf (xml, "</%s>", node->name);
c400dd
 
c400dd
   for (n = node->next; n; n = n->next)
c400dd
     {
c400dd
       char *sibling = rest_xml_node_print (n);
c400dd
 
c400dd
-      xml = g_strconcat (xml, sibling, NULL);
c400dd
+      g_string_append (xml, sibling);
c400dd
       g_free (sibling);
c400dd
     }
c400dd
 
c400dd
-  return xml;
c400dd
+  return g_string_free (xml, FALSE);
c400dd
 }
c400dd
 
c400dd
 /**
c400dd
-- 
c400dd
2.14.2
c400dd
c400dd
c400dd
From a34d02947c4f102e6d16b9d328941a4b2946c8e8 Mon Sep 17 00:00:00 2001
c400dd
From: Debarshi Ray <debarshir@gnome.org>
c400dd
Date: Fri, 13 Oct 2017 18:53:39 +0200
c400dd
Subject: [PATCH 2/4] xml-node: Remove stray blank space
c400dd
c400dd
This had broken tests/xml.c.
c400dd
c400dd
Fallout from 61a7b231bd8b9d1b8d02dca120389e79d38b428d
c400dd
c400dd
https://bugzilla.gnome.org/show_bug.cgi?id=788960
c400dd
---
c400dd
 rest/rest-xml-node.c | 2 +-
c400dd
 1 file changed, 1 insertion(+), 1 deletion(-)
c400dd
c400dd
diff --git a/rest/rest-xml-node.c b/rest/rest-xml-node.c
c400dd
index a8156dbbd432..d3a7c995affd 100644
c400dd
--- a/rest/rest-xml-node.c
c400dd
+++ b/rest/rest-xml-node.c
c400dd
@@ -291,7 +291,7 @@ rest_xml_node_print (RestXmlNode *node)
c400dd
 
c400dd
   g_hash_table_iter_init (&iter, node->attrs);
c400dd
   while (g_hash_table_iter_next (&iter, &key, &value))
c400dd
-    g_string_append_printf (xml, " %s =\'%s\'", (char *)key, (char *)value);
c400dd
+    g_string_append_printf (xml, " %s=\'%s\'", (char *)key, (char *)value);
c400dd
 
c400dd
   g_string_append (xml, ">");
c400dd
 
c400dd
-- 
c400dd
2.14.2
c400dd
c400dd
c400dd
From f184db2bff0618b99c4de3316082fe80439f124c Mon Sep 17 00:00:00 2001
c400dd
From: Debarshi Ray <debarshir@gnome.org>
c400dd
Date: Fri, 13 Oct 2017 19:14:16 +0200
c400dd
Subject: [PATCH 3/4] xml-node: Define the order in which attributes & children
c400dd
 are printed
c400dd
c400dd
The order in which GHashTable returns its key-value pairs is undefined.
c400dd
Therefore the output of rest_xml_node_print can change based on the
c400dd
GHashTable implementation. While not strictly necessary, it would be
c400dd
nice to avoid that. Having a stable order, even if it is not
c400dd
documented and depends on the current RestXmlNode code, is handy for
c400dd
testing.
c400dd
c400dd
This was the main reason behind the tests/xml.c breakage.
c400dd
c400dd
https://bugzilla.gnome.org/show_bug.cgi?id=788960
c400dd
---
c400dd
 rest/rest-xml-node.c | 23 ++++++++++++++++++++++-
c400dd
 1 file changed, 22 insertions(+), 1 deletion(-)
c400dd
c400dd
diff --git a/rest/rest-xml-node.c b/rest/rest-xml-node.c
c400dd
index d3a7c995affd..973ebcf6c3fa 100644
c400dd
--- a/rest/rest-xml-node.c
c400dd
+++ b/rest/rest-xml-node.c
c400dd
@@ -283,6 +283,9 @@ rest_xml_node_print (RestXmlNode *node)
c400dd
 {
c400dd
   GHashTableIter iter;
c400dd
   gpointer       key, value;
c400dd
+  GList          *attrs = NULL;
c400dd
+  GList          *children = NULL;
c400dd
+  GList          *l;
c400dd
   GString        *xml = g_string_new (NULL);
c400dd
   RestXmlNode   *n;
c400dd
 
c400dd
@@ -291,13 +294,29 @@ rest_xml_node_print (RestXmlNode *node)
c400dd
 
c400dd
   g_hash_table_iter_init (&iter, node->attrs);
c400dd
   while (g_hash_table_iter_next (&iter, &key, &value))
c400dd
-    g_string_append_printf (xml, " %s=\'%s\'", (char *)key, (char *)value);
c400dd
+    {
c400dd
+      char *attr = g_strdup_printf ("%s=\'%s\'", (char *)key, (char *)value);
c400dd
+      attrs = g_list_prepend (attrs, attr);
c400dd
+    }
c400dd
+
c400dd
+  attrs = g_list_sort (attrs, (GCompareFunc) g_strcmp0);
c400dd
+  for (l = attrs; l; l = l->next)
c400dd
+    {
c400dd
+      const char *attr = (const char *) l->data;
c400dd
+      g_string_append_printf (xml, " %s", attr);
c400dd
+    }
c400dd
 
c400dd
   g_string_append (xml, ">");
c400dd
 
c400dd
   g_hash_table_iter_init (&iter, node->children);
c400dd
   while (g_hash_table_iter_next (&iter, &key, &value))
c400dd
+    children = g_list_prepend (children, key);
c400dd
+
c400dd
+  children = g_list_sort (children, (GCompareFunc) g_strcmp0);
c400dd
+  for (l = children; l; l = l->next)
c400dd
     {
c400dd
+      const char *name = (const char *) l->data;
c400dd
+      RestXmlNode *value = (RestXmlNode *) g_hash_table_lookup (node->children, name);
c400dd
       char *child = rest_xml_node_print ((RestXmlNode *) value);
c400dd
 
c400dd
       g_string_append (xml, child);
c400dd
@@ -317,6 +336,8 @@ rest_xml_node_print (RestXmlNode *node)
c400dd
       g_free (sibling);
c400dd
     }
c400dd
 
c400dd
+  g_list_free_full (attrs, g_free);
c400dd
+  g_list_free (children);
c400dd
   return g_string_free (xml, FALSE);
c400dd
 }
c400dd
 
c400dd
-- 
c400dd
2.14.2
c400dd
c400dd
c400dd
From e5ee6ef751ee5a38d7b9fadcd631cf6ecec7b240 Mon Sep 17 00:00:00 2001
c400dd
From: Debarshi Ray <debarshir@gnome.org>
c400dd
Date: Fri, 13 Oct 2017 19:16:55 +0200
c400dd
Subject: [PATCH 4/4] tests: Re-enable the XML test
c400dd
c400dd
This reverts commit 2d1dbfe7073b1e153ff881426b40a9a517fb796b
c400dd
c400dd
https://bugzilla.gnome.org/show_bug.cgi?id=788960
c400dd
---
c400dd
 tests/Makefile.am | 2 --
c400dd
 1 file changed, 2 deletions(-)
c400dd
c400dd
diff --git a/tests/Makefile.am b/tests/Makefile.am
c400dd
index 5d77f9cf5445..5ffdd4634e9a 100644
c400dd
--- a/tests/Makefile.am
c400dd
+++ b/tests/Makefile.am
c400dd
@@ -1,6 +1,4 @@
c400dd
 TESTS = proxy proxy-continuous threaded oauth oauth-async oauth2 flickr lastfm xml custom-serialize
c400dd
-# TODO: fix this test case
c400dd
-XFAIL_TESTS = xml
c400dd
 
c400dd
 AM_CPPFLAGS = $(SOUP_CFLAGS) -I$(top_srcdir) $(GCOV_CFLAGS)
c400dd
 AM_LDFLAGS = $(SOUP_LIBS) $(GCOV_LDFLAGS) \
c400dd
-- 
c400dd
2.14.2
c400dd