After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 491651 - xmlMemFree(NULL) crashes
xmlMemFree(NULL) crashes
Status: RESOLVED FIXED
Product: libxml2
Classification: Platform
Component: general
2.6.x
Other All
: Normal minor
: ---
Assigned To: Daniel Veillard
libxml QA maintainers
Depends on:
Blocks:
 
 
Reported: 2007-10-30 09:47 UTC by Alexey Proskuryakov
Modified: 2007-10-30 20:27 UTC
See Also:
GNOME target: ---
GNOME version: ---



Description Alexey Proskuryakov 2007-10-30 09:47:57 UTC
xmlMemFree is documented as "a free() equivalent, with error checking". AFAIK, at least C99 explicitly specifies that free(NULL) has no action.

    p = CLIENT_2_HDR(ptr);  // p is negative
    if (p->mh_tag != MEMTAG) { // crashes

This problem makes Safari/WebKit crash on any XHTML file when running against a libxml2 built with memory debugging enabled. Production builds are of course OK, since xmlFree() maps to system free() there.
Comment 1 Daniel Veillard 2007-10-30 11:11:38 UTC
Hum, we could add a test of NULL in that function, that 
doesn't sound a problem to me, but libxml2 library is 
designed to never call xmlFree(NULL), that allow to
spot more errors.

Daniel
Comment 2 Alexey Proskuryakov 2007-10-30 12:23:53 UTC
In WebKit, calling xmlFree(NULL) happens naturally, e.g.:

xmlChar* base = xmlNodeGetBase(parentDoc, (xmlNodePtr)parentDoc);
// ...
xmlFree(base);

Adding null checks around xmlFree calls would be undesirable for us for performance reasons, and, perhaps more importantly, this is not a stable solution in the sense that missing checks won't be detected when using production libxml2 builds. Also, most of the code base uses versions of delete/free that work with null parameters, so it would be hard to remember about null checks around xmlFree.
Comment 3 Daniel Veillard 2007-10-30 20:27:02 UTC
Sure, I understand, I was speaking for libxml2 internal code.
For the debug mode your problem should be trivially fixed by
the following patch

paphio:~/XML -> svn diff
Index: xmlmemory.c
===================================================================
--- xmlmemory.c (revision 3660)
+++ xmlmemory.c (working copy)
@@ -409,6 +409,9 @@
     size_t size;
 #endif
 
+    if (ptr == NULL)
+       return;
+
     if (ptr == (void *) -1) {
        xmlGenericError(xmlGenericErrorContext,
            "trying to free pointer from freed area\n");
paphio:~/XML -> 

  which I commited to SVN head,

Daniel