Commits

ph...@9ae0c189-cd1f-4510-a509-f4891f5cf20d  committed 26b7cfb

Modified Files:
dom.c
+ fixes a libxml2 oddity, that occours with parsing of balanced chunks:
nested nodes will contain a reference to an internal, but no longer
existing document. the newly created internal function fixes the
wrong pointer to the correct ones.

  • Participants
  • Parent commits d58091c

Comments (0)

Files changed (1)

 
 #include <stdio.h>
 
+#ifdef XS_WARNINGS
+#define xs_warn(string) fprintf(stderr, string) 
+#else
+#define xs_warn(string)
+#endif
+
 xmlDocPtr
 domCreateDocument( xmlChar *version, xmlChar *enc ){
     xmlDocPtr doc = NULL;
  * in 99% the cases i believe it is faster than to create the dom by hand,
  * and skip the parsing job which has to be done here.
  **/
+void 
+_fix_node( xmlNodePtr node, xmlDocPtr doc ) {
+    /* internal helper for domReadWellBalancedString */
+
+    /* libxml2 creates an internal document for the chunk. this is ok
+       for flat chunks. chunks with nested chunks will keep a
+       reference to that internal document. this will lead into
+       segfaults, if the child nodes are not fixed. */
+
+    if ( node != NULL
+         && node->type != XML_DTD_NODE
+         && node->type != XML_NAMESPACE_DECL 
+         && node->doc != NULL ) {
+        node->doc = doc;
+
+        if ( node->children != NULL )
+            _fix_node( node->children, doc );
+
+        _fix_node( node->next , doc );
+    }
+}
+
 xmlNodePtr 
 domReadWellBalancedString( xmlDocPtr doc, xmlChar* block ) {
     int retCode       = -1;
                 nodes = helper;
             }
         }
+        else {
+            _fix_node( nodes, doc );
+        }
     }
 
     return nodes;
                 ret = xmlStrdup( string );
             }
             else if ( enc == XML_CHAR_ENCODING_ERROR ){
+                xs_warn("no standard encoding\n");
                 coder = xmlFindCharEncodingHandler( encoding );
             }
             else {
-                /* fprintf(stderr, "NO XML ENCODING!\n"); */ 
+                xs_warn("no encoding found\n");
             }
 
             if ( coder != NULL ) {
+                xs_warn("coding machine found\n");
                 in    = xmlBufferCreate();
                 out   = xmlBufferCreate();
                 xmlBufferCCat( in, string );
                     ret = xmlStrdup( out->content );
                 }
                 else {
-                    /* fprintf(stderr, "b0rked encoiding!\n"); */
+                     xs_warn( "b0rked encoiding!\n");
                 }
                     
                 xmlBufferFree( in );
                 xmlBufferFree( out );
             }
             else {
-                /*  fprintf(stderr, "no coder found\n"); */
+                xs_warn("no coder found\n");
             }
         }
         else {
                 coder = xmlFindCharEncodingHandler( encoding );
             }
             else {
-                /*  fprintf(stderr, "NO XML ENCODING!\n"); */
+                xs_warn("no encoding found");
             }
 
             if ( coder != NULL ) {