[xml] bug in xmlSetProp?



Hi,

I'm new to the list but have been using libxml for quite some time now.
It's a great library and it's saved me a ton of time.  I found an
interesting bug though and downloaded the newest version of libxml2
(2.4.10) and verified that it still there.  The only problem is that
this might be more of a bug in how I'm using the library.

I like to transverse the tree myself.  I also like to look up the values
myself to reduce the amount of memory that beings copied (I got too much
C in my blood it seems).  When working on some prototype code, I ran
across a memory corruption when encountering code such as this:

xmlNodePtr node = xmlNewNode(0, BAD_CAST "test");
xmlAttrPtr prop = xmlSetProp(node, BAD_CAST "test", BAD_CAST "test");

xmlSetProp(node, BAD_CAST "test", prop->children->content);

Now, this alone doesn't appear to cause any harm but trust me, it's
copying free'd memory.  I have some code that is rather long that can
reproduce it reliably with corruption so if anyone wants it, I can
provide it.  The problem is simple and easy to fix but I do not know if
the real problem is how I am approaching this.

I would think that this should be legal, although I know probably
discouraged.  Either way, the fix is just to return prop is the passed
in value is equal to the prop->children->content.  A more proper way
might be to see if value is equal to that property then return that
property instead of just creating a new one.  That would be pretty
expensive though.

2001-11-24 12:34             diff -lc tree.c~ tree.c              Page 1



*** tree.c~ Fri Nov 23 21:33:54 2001
--- tree.c Sat Nov 24 12:33:32 2001
***************
*** 4492,4497 ****
--- 4492,4499 ----
      while (prop != NULL) {
          if ((xmlStrEqual(prop->name, name)) &&
       (prop->ns == NULL)){
+      if (prop->children->content == value)
+          return (prop);
       if (prop->children != NULL)
           xmlFreeNodeList(prop->children);
       prop->children = NULL;

Regards,
Anthony Liguori




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