[xml] bug in xmlTextWriterWriteCDATA



Please let me know if this mailing list is not the right mechanism for
submitting patches.

The current version of xmlwriter.c fails to emit CDATA section when
there are characters between parent XML element and the CDATA section
itself, as in:

   <foo>   <![CDATA[bar]]>  </foo>

The patch below fixes it:

*** xmlwriter.c.orig    2005-10-05 17:35:10.000000000 -0400
--- xmlwriter.c 2005-10-04 17:03:18.000000000 -0400
***************
*** 2536,2610 ****
   * Returns the bytes written (may be 0 because of buffering) or -1
in case of error
   */
  int
  xmlTextWriterStartCDATA(xmlTextWriterPtr writer)
  {
      int count;
      int sum;
      xmlLinkPtr lk;
      xmlTextWriterStackEntry *p;

      if (writer == NULL)
          return -1;

      sum = 0;
      lk = xmlListFront(writer->nodes);
      if (lk != 0) {
          p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
          if (p != 0) {
              switch (p->state) {
                  case XML_TEXTWRITER_NONE:
                  case XML_TEXTWRITER_PI:
                  case XML_TEXTWRITER_PI_TEXT:
                      break;
                  case XML_TEXTWRITER_ATTRIBUTE:
                      count = xmlTextWriterEndAttribute(writer);
                      if (count < 0)
                          return -1;
                      sum += count;
                      /* fallthrough */
                  case XML_TEXTWRITER_NAME:
                      count = xmlOutputBufferWriteString(writer->out, ">");
                      if (count < 0)
                          return -1;
                      sum += count;
                      p->state = XML_TEXTWRITER_TEXT;
                      break;
                  case XML_TEXTWRITER_CDATA:
                      xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
                                      "xmlTextWriterStartCDATA : CDATA
not allowed in this context!\n");
                      return -1;
                  default:
                      return -1;
              }
          }
      }

      p = (xmlTextWriterStackEntry *)
          xmlMalloc(sizeof(xmlTextWriterStackEntry));
      if (p == 0) {
          xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
                          "xmlTextWriterStartCDATA : out of memory!\n");
          return -1;
      }

      p->name = NULL;
      p->state = XML_TEXTWRITER_CDATA;

      xmlListPushFront(writer->nodes, p);

      count = xmlOutputBufferWriteString(writer->out, "<![CDATA[");
      if (count < 0)
          return -1;
      sum += count;

      return sum;
  }

  /**
   * xmlTextWriterEndCDATA:
   * @writer:  the xmlTextWriterPtr
   *
   * End an xml CDATA section.
   *
   * Returns the bytes written (may be 0 because of buffering) or -1
in case of error
   */
--- 2536,2611 ----
   * Returns the bytes written (may be 0 because of buffering) or -1
in case of error
   */
  int
  xmlTextWriterStartCDATA(xmlTextWriterPtr writer)
  {
      int count;
      int sum;
      xmlLinkPtr lk;
      xmlTextWriterStackEntry *p;

      if (writer == NULL)
          return -1;

      sum = 0;
      lk = xmlListFront(writer->nodes);
      if (lk != 0) {
          p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
          if (p != 0) {
              switch (p->state) {
                  case XML_TEXTWRITER_NONE:
+                 case XML_TEXTWRITER_TEXT:
                  case XML_TEXTWRITER_PI:
                  case XML_TEXTWRITER_PI_TEXT:
                      break;
                  case XML_TEXTWRITER_ATTRIBUTE:
                      count = xmlTextWriterEndAttribute(writer);
                      if (count < 0)
                          return -1;
                      sum += count;
                      /* fallthrough */
                  case XML_TEXTWRITER_NAME:
                      count = xmlOutputBufferWriteString(writer->out, ">");
                      if (count < 0)
                          return -1;
                      sum += count;
                      p->state = XML_TEXTWRITER_TEXT;
                      break;
                  case XML_TEXTWRITER_CDATA:
                      xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
                                      "xmlTextWriterStartCDATA : CDATA
not allowed in this context!\n");
                      return -1;
                  default:
                      return -1;
              }
          }
      }

      p = (xmlTextWriterStackEntry *)
          xmlMalloc(sizeof(xmlTextWriterStackEntry));
      if (p == 0) {
          xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
                          "xmlTextWriterStartCDATA : out of memory!\n");
          return -1;
      }

      p->name = NULL;
      p->state = XML_TEXTWRITER_CDATA;

      xmlListPushFront(writer->nodes, p);

      count = xmlOutputBufferWriteString(writer->out, "<![CDATA[");
      if (count < 0)
          return -1;
      sum += count;

      return sum;
  }

  /**
   * xmlTextWriterEndCDATA:
   * @writer:  the xmlTextWriterPtr
   *
   * End an xml CDATA section.
   *
   * Returns the bytes written (may be 0 because of buffering) or -1
in case of error
   */



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