Re: [xml] bug in xmlSetProp?



On Sat, Nov 24, 2001 at 12:37:45PM -0500, Anthony Liguori wrote:
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

  Very simple. the proper API to get the value of a node property
is xmlGetProp(). If you don't do this you're on your own. Same if you
start playing with node links (parent/children/last ...) you can perfectly
create loops, I allow access but you'd better know what you're doing if
you do this.
  I allow accessing the content structure directly, because I'm a C
programmer myself and I know why someone else my like to do this, but
it's C, it mean you need to know the context fairly well. And while
prop->children->content may be the actual value of the attribute in
most cases it's not as soon as you have an entity reference made in
the attribute value (libxml is different from nearly every other toolkits
in the sense it preserves and saves back entities references from
attribute values).

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.

   Yes, and I prefer to avoid adding tons of checks like this. If
you know what you're doing, then prop->children->content might be useful.
If unsure, use the API xmlGetProp()

Daniel

-- 
Daniel Veillard      | Red Hat Network https://rhn.redhat.com/
veillard redhat com  | libxml Gnome XML XSLT toolkit  http://xmlsoft.org/
http://veillard.com/ | Rpmfind RPM search engine http://rpmfind.net/



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