[xml] xmlNodeAddContent causes segmentation violation error



Dear All,

   Many thanks to the responses I got about my last trouble with
XInclude, I appreciate the efforts of the several people who got back
to me so quickly.

   I'm wondering if I can ask for some more help.  I'm up against
another mysterious SIGSEGV in a pretty simple situation:

   (1) Here is the source code "trouble.c":

--------------------------------------------------------------------------------
#include <stdlib.h>
#include <stdio.h>
#include <libxml/parser.h>

int main(int argc, char *argv[]) {

  xmlDoc *doc ;
  xmlNode *node ;
  xmlParserOption options = XML_PARSE_XINCLUDE ;
  
  doc = xmlReadFile(argv[1],NULL,options) ;
  node = doc->children->children->next->children->next->children->next->children ;
  printf("address: %p content: \"%s\"\n",node,node->content) ;
  xmlNodeAddContent(node,"FOO") ;
  printf("address: %p content: \"%s\"\n",node,node->content) ;
  return(0) ;
}
================================================================================

   (2) Here is the compiling command:
--------------------------------------------------------------------------------
gcc -g -Wall `xml2-config --cflags` trouble.c `xml2-config --libs`
================================================================================

   (3) Here is the first input file (trouble.xml).  The long chain of 
pointers in the program above points to the XML_TEXT_NODE just after the 
<text> node, which has the content "When is a ":
--------------------------------------------------------------------------------
<?xml version="1.0"?>
<xques>
<class name="tree">
<question>
<text>When is a <var name="noun">cigar</var> just a cigar?</text>
</question>
</class>
</xques>
================================================================================

   (4) Here is the resulting output, perfectly normal:
--------------------------------------------------------------------------------
a.out trouble.xml
address: 0x805bd98 content: "When is a "
address: 0x805bd98 content: "When is a FOO"
================================================================================

   (5) Here is the second input file (trouble2.xml).  The only change from
the first input file is that the XML_TEXT_NODE has the content 
"\nWhen is a ", that is, a carriage return has been added to the beginning:
--------------------------------------------------------------------------------
<?xml version="1.0"?>
<xques>
<class name="tree">
<question>
<text>
When is a <var name="noun">cigar</var> just a cigar?</text>
</question>
</class>
</xques>
================================================================================

   (6) Here is the resulting output:
--------------------------------------------------------------------------------
a.out trouble2.xml
address: 0x805bd98 content: "
When is a "
Segmentation fault (core dumped)
================================================================================

   If I link to Electric Fence or build libxml2 with mem-debug
support, both programs tell me that the address of the XML_TEXT_NODE's
content field, when passed to realloc or xmlRealloc, was flagged as
not coming originally from malloc.

   Any help is, again, appreciated.

   CJG



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