Fixes for libxml strings

View: New views
2 Messages — Rating Filter:   Alert me  

Fixes for libxml strings

by Gustavo Sverzut Barbieri-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,

While debugging my code using glib, libxml and gdome, both compiled with
memory pools disabled and in a garbage collector friendly way, I found that
some Gdome functions misuse g_free() instead of xmlFree().

Main problem is using gdome_str_mkref_own() with libxml returned values. Since
libxml have its own memory stuff it fails when using glib g_free(). I just
replaced them with gdome_str_mkref_xml(), which will use xmlFree() instead of
g_free().

Attached is the patch to fix issues I have encountered so far.

Both 0.8.1 and CVS have the same problem.

--
Gustavo Sverzut Barbieri
------------------------
Instituto Nokia de Tecnologia - INdT

Jabber: barbieri@...
   MSN: barbieri@...
  ICQ#: 17249123
 Skype: gsbarbieri
Mobile: +55 (81) 9927 0010
 Phone:  +1 (347) 624 6296; 08122692@...
   GPG: 0xB640E1A2 @ wwwkeys.pgp.net

[gdome2-cvs-xmlFree.patch]

diff -ur gdome2/libgdome/gdomecore/gdome-xml-attribute.c gdome2-0.8.1-fix/libgdome/gdomecore/gdome-xml-attribute.c
--- gdome2/libgdome/gdomecore/gdome-xml-attribute.c 2003-07-18 08:51:20.000000000 -0300
+++ gdome2-0.8.1-fix/libgdome/gdomecore/gdome-xml-attribute.c 2006-03-28 12:29:43.000000000 -0300
@@ -249,7 +249,7 @@
   value = xmlNodeGetContent ((xmlNode *)priv->n);
 
   if (value != NULL)
-    return gdome_xml_str_mkref_own (value);
+    return gdome_xml_str_mkref_xml (value);
   else
     return gdome_xml_str_mkref_dup("");
 }
diff -ur gdome2/libgdome/gdomecore/gdome-xml-cdata.c gdome2-0.8.1-fix/libgdome/gdomecore/gdome-xml-cdata.c
--- gdome2/libgdome/gdomecore/gdome-xml-cdata.c 2003-07-18 08:51:20.000000000 -0300
+++ gdome2-0.8.1-fix/libgdome/gdomecore/gdome-xml-cdata.c 2006-03-28 12:57:46.000000000 -0300
@@ -185,7 +185,7 @@
  g_return_val_if_fail (GDOME_XML_IS_CD (priv), NULL);
  g_return_val_if_fail (exc != NULL, NULL);
 
- return gdome_xml_str_mkref_own (xmlNodeGetContent (priv->n));
+ return gdome_xml_str_mkref_xml (xmlNodeGetContent (priv->n));
 }
 
 /**
@@ -268,7 +268,7 @@
 
  content = xmlNodeGetContent (priv->n);
  length = gdome_utf16Length (content);
- g_free (content);
+ xmlFree (content);
  return length;
 }
 
@@ -360,8 +360,8 @@
  if (gdome_xml_n_eventEnabledByCode((GdomeNode *) self, DOM_CHARACTER_DATA_MODIFIED_EVENT_TYPE)) {
  /* Fire DOMCharacterDataModified */
  mev = gdome_evt_mevnt_mkref ();
- prevValue = gdome_xml_str_mkref (old_str);
- newValue = gdome_xml_str_mkref (new_str);
+ prevValue = gdome_xml_str_mkref_xml (old_str);
+ newValue = gdome_xml_str_mkref_xml (new_str);
  gdome_evt_mevnt_initMutationEventByCode (mev, DOM_CHARACTER_DATA_MODIFIED_EVENT_TYPE,
  TRUE, FALSE, NULL,
  prevValue, newValue, NULL, 0, exc);
@@ -370,7 +370,7 @@
  gdome_xml_str_unref (prevValue);
  gdome_evt_mevnt_unref ((GdomeEvent *)mev, exc);
  }
- g_free (new_str);
+ xmlFree (new_str);
 
   if (old_str != NULL)
     xmlFree (old_str);
diff -ur gdome2/libgdome/gdomecore/gdome-xml-element.c gdome2-0.8.1-fix/libgdome/gdomecore/gdome-xml-element.c
--- gdome2/libgdome/gdomecore/gdome-xml-element.c 2003-10-03 12:53:32.000000000 -0300
+++ gdome2-0.8.1-fix/libgdome/gdomecore/gdome-xml-element.c 2006-03-28 12:50:17.000000000 -0300
@@ -196,7 +196,7 @@
  value = xmlGetProp (priv->n, name->str);
 
  if (value != NULL)
- return gdome_xml_str_mkref_own (value);
+ return gdome_xml_str_mkref_xml (value);
  else {
  strs = g_strsplit((gchar *)name->str, ":", 0);
  if (xmlStrEqual(strs[0], "xmlns"))
diff -ur gdome2/libgdome/gdomecore/gdome-xml-str.c gdome2-0.8.1-fix/libgdome/gdomecore/gdome-xml-str.c
--- gdome2/libgdome/gdomecore/gdome-xml-str.c 2002-04-01 14:08:24.000000000 -0300
+++ gdome2-0.8.1-fix/libgdome/gdomecore/gdome-xml-str.c 2006-03-28 12:59:10.000000000 -0300
@@ -42,7 +42,7 @@
 
 void
 gdome_xml_str_unref_own (GdomeDOMString *self) {
- g_free (self->str);
+ g_free (self->str);
 #ifdef DEBUG_REFCNT
       gdome_refdbg_delRef ((void *)self, GDOME_REFDBG_STR);
 #endif
diff -ur gdome2/libgdome/gdomecore/gdome-xml-xmlutil.c gdome2-0.8.1-fix/libgdome/gdomecore/gdome-xml-xmlutil.c
--- gdome2/libgdome/gdomecore/gdome-xml-xmlutil.c 2003-10-03 12:53:32.000000000 -0300
+++ gdome2-0.8.1-fix/libgdome/gdomecore/gdome-xml-xmlutil.c 2006-03-28 12:45:42.000000000 -0300
@@ -975,9 +975,9 @@
  cur->doc = doc;
  }
  if (prefix == NULL) {
- cur->name = g_strdup("xmlns");
+ cur->name = xmlStrdup ("xmlns");
  } else {
- cur->name = g_strdup(prefix);
+ cur->name = xmlStrdup (prefix);
  cur->ns = gdome_xmlNewNs (doc, GDOME_XMLNS_NAMESPACE, "xmlns");
  }
  if (value != NULL) {


[gdome2-0.8.1-xmlFree.patch]

diff -ur gdome2-0.8.1/libgdome/gdomecore/gdome-xml-attribute.c gdome2-0.8.1-fix/libgdome/gdomecore/gdome-xml-attribute.c
--- gdome2-0.8.1/libgdome/gdomecore/gdome-xml-attribute.c 2003-07-18 08:51:21.000000000 -0300
+++ gdome2-0.8.1-fix/libgdome/gdomecore/gdome-xml-attribute.c 2006-03-28 12:29:43.000000000 -0300
@@ -249,7 +249,7 @@
   value = xmlNodeGetContent ((xmlNode *)priv->n);
 
   if (value != NULL)
-    return gdome_xml_str_mkref_own (value);
+    return gdome_xml_str_mkref_xml (value);
   else
     return gdome_xml_str_mkref_dup("");
 }
diff -ur gdome2-0.8.1/libgdome/gdomecore/gdome-xml-cdata.c gdome2-0.8.1-fix/libgdome/gdomecore/gdome-xml-cdata.c
--- gdome2-0.8.1/libgdome/gdomecore/gdome-xml-cdata.c 2003-07-18 08:51:21.000000000 -0300
+++ gdome2-0.8.1-fix/libgdome/gdomecore/gdome-xml-cdata.c 2006-03-28 12:57:46.000000000 -0300
@@ -185,7 +185,7 @@
  g_return_val_if_fail (GDOME_XML_IS_CD (priv), NULL);
  g_return_val_if_fail (exc != NULL, NULL);
 
- return gdome_xml_str_mkref_own (xmlNodeGetContent (priv->n));
+ return gdome_xml_str_mkref_xml (xmlNodeGetContent (priv->n));
 }
 
 /**
@@ -268,7 +268,7 @@
 
  content = xmlNodeGetContent (priv->n);
  length = gdome_utf16Length (content);
- g_free (content);
+ xmlFree (content);
  return length;
 }
 
@@ -360,8 +360,8 @@
  if (gdome_xml_n_eventEnabledByCode((GdomeNode *) self, DOM_CHARACTER_DATA_MODIFIED_EVENT_TYPE)) {
  /* Fire DOMCharacterDataModified */
  mev = gdome_evt_mevnt_mkref ();
- prevValue = gdome_xml_str_mkref (old_str);
- newValue = gdome_xml_str_mkref (new_str);
+ prevValue = gdome_xml_str_mkref_xml (old_str);
+ newValue = gdome_xml_str_mkref_xml (new_str);
  gdome_evt_mevnt_initMutationEventByCode (mev, DOM_CHARACTER_DATA_MODIFIED_EVENT_TYPE,
  TRUE, FALSE, NULL,
  prevValue, newValue, NULL, 0, exc);
@@ -370,7 +370,7 @@
  gdome_xml_str_unref (prevValue);
  gdome_evt_mevnt_unref ((GdomeEvent *)mev, exc);
  }
- g_free (new_str);
+ xmlFree (new_str);
 
   if (old_str != NULL)
     xmlFree (old_str);
diff -ur gdome2-0.8.1/libgdome/gdomecore/gdome-xml-element.c gdome2-0.8.1-fix/libgdome/gdomecore/gdome-xml-element.c
--- gdome2-0.8.1/libgdome/gdomecore/gdome-xml-element.c 2003-10-03 12:53:33.000000000 -0300
+++ gdome2-0.8.1-fix/libgdome/gdomecore/gdome-xml-element.c 2006-03-28 12:50:17.000000000 -0300
@@ -196,7 +196,7 @@
  value = xmlGetProp (priv->n, name->str);
 
  if (value != NULL)
- return gdome_xml_str_mkref_own (value);
+ return gdome_xml_str_mkref_xml (value);
  else {
  strs = g_strsplit((gchar *)name->str, ":", 0);
  if (xmlStrEqual(strs[0], "xmlns"))
diff -ur gdome2-0.8.1/libgdome/gdomecore/gdome-xml-str.c gdome2-0.8.1-fix/libgdome/gdomecore/gdome-xml-str.c
--- gdome2-0.8.1/libgdome/gdomecore/gdome-xml-str.c 2002-04-01 14:08:24.000000000 -0300
+++ gdome2-0.8.1-fix/libgdome/gdomecore/gdome-xml-str.c 2006-03-28 12:59:10.000000000 -0300
@@ -42,7 +42,7 @@
 
 void
 gdome_xml_str_unref_own (GdomeDOMString *self) {
- g_free (self->str);
+ g_free (self->str);
 #ifdef DEBUG_REFCNT
       gdome_refdbg_delRef ((void *)self, GDOME_REFDBG_STR);
 #endif
diff -ur gdome2-0.8.1/libgdome/gdomecore/gdome-xml-xmlutil.c gdome2-0.8.1-fix/libgdome/gdomecore/gdome-xml-xmlutil.c
--- gdome2-0.8.1/libgdome/gdomecore/gdome-xml-xmlutil.c 2003-10-03 12:53:33.000000000 -0300
+++ gdome2-0.8.1-fix/libgdome/gdomecore/gdome-xml-xmlutil.c 2006-03-28 12:45:42.000000000 -0300
@@ -975,9 +975,9 @@
  cur->doc = doc;
  }
  if (prefix == NULL) {
- cur->name = g_strdup("xmlns");
+ cur->name = xmlStrdup ("xmlns");
  } else {
- cur->name = g_strdup(prefix);
+ cur->name = xmlStrdup (prefix);
  cur->ns = gdome_xmlNewNs (doc, GDOME_XMLNS_NAMESPACE, "xmlns");
  }
  if (value != NULL) {


_______________________________________________
gdome mailing list
gdome@...
http://mail.gnome.org/mailman/listinfo/gdome

Parent Message unknown Re: Fixes for libxml strings

by Gustavo Sverzut Barbieri-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wednesday 29 March 2006 00:11, ext Sameer Oak wrote:
> > ... replaced them with gdome_str_mkref_xml(), which will use xmlFree()
> > instead of g_free().
>
> ---
> Was it gdome_str_unref()?

Sorry, I don't understand what you mean.

gdome_str_unref() will call the unref function provided by the
gdome_str_mkref*() functions.

If you use gdome_str_mkref(), no free() on ptr->str.

If you use gdome_str_mkref_own() or gdome_str_mkref_dup(), g_free( ptr->str )
will be used.

If you use gdome_str_mkref_xml(), xmlFree( ptr->str ) will be used.


Yes, they're different because libxml and glib uses different allocator and if
you misuse them you can get things like double-free or memory leaks.



--
Gustavo Sverzut Barbieri
------------------------
Instituto Nokia de Tecnologia - INdT

Jabber: barbieri@...
   MSN: barbieri@...
  ICQ#: 17249123
 Skype: gsbarbieri
Mobile: +55 (81) 9927 0010
 Phone:  +1 (347) 624 6296; 08122692@...
   GPG: 0xB640E1A2 @ wwwkeys.pgp.net
_______________________________________________
gdome mailing list
gdome@...
http://mail.gnome.org/mailman/listinfo/gdome