[xslt] xsl:include thing



Hello there.

Here is the patch which removes the crash caused by an invalid file
referenced in xsl:include element.

What happened is the following:
* xsltParseStylesheetProcess: Modified not to free the stylesheet which it
receives as a parameter.
* xsltParseStylesheetDoc: Modified to free the stylesheet it now carries the
responsibility for
* xsltParseStylesheetInclude: Added an int return value to indicate failure.
* xsltParseStylesheetImport: Added an int return value to indicate failure,
fixed memory leak incase of error
* xsltParseStylesheetTop: Added check for these return values and indicated
an errorneous XSL document accordingly.

The damn diff obviously also contains few differences which have nothing to
do with the code. My xemacs has a different indenting scheme and a different
tab size than what Daniel used to make this file. I am still not that
familiar with the tool to be able to teach it new identing :-) I am
learning.

Apart from that, if the patch is okay for everyone, I shall apply it.

Ciao
Igor



Index: imports.c
===================================================================
RCS file: /cvs/gnome/libxslt/libxslt/imports.c,v
retrieving revision 1.12
diff -c -r1.12 imports.c
*** imports.c 14 Apr 2002 13:35:23 -0000 1.12
--- imports.c 14 May 2002 17:04:32 -0000
***************
*** 58,68 ****
   * @template:  the "strip-space" element
   *
   * parse an XSLT stylesheet strip-space element and record
!  * elements needing stripping
   */

! void
  xsltParseStylesheetImport(xsltStylesheetPtr style, xmlNodePtr cur) {
      xmlDocPtr import = NULL;
      xmlChar *base = NULL;
      xmlChar *uriRef = NULL;
--- 58,70 ----
   * @template:  the "strip-space" element
   *
   * parse an XSLT stylesheet strip-space element and record
!  * elements needing stripping. Returns zero on success and something else
!  * on failure.
   */

! int
  xsltParseStylesheetImport(xsltStylesheetPtr style, xmlNodePtr cur) {
+     int ret = -1;
      xmlDocPtr import = NULL;
      xmlChar *base = NULL;
      xmlChar *uriRef = NULL;
***************
*** 70,76 ****
      xsltStylesheetPtr res;

      if ((cur == NULL) || (style == NULL))
!  return;

      uriRef = xsltGetNsProp(cur, (const xmlChar *)"href", XSLT_NAMESPACE);
      if (uriRef == NULL) {
--- 72,78 ----
      xsltStylesheetPtr res;

      if ((cur == NULL) || (style == NULL))
!  return (ret);

      uriRef = xsltGetNsProp(cur, (const xmlChar *)"href", XSLT_NAMESPACE);
      if (uriRef == NULL) {
***************
*** 102,108 ****
   res->next = style->imports;
   style->imports = res;
   style->extrasNr += res->extrasNr;
!     }

  error:
      if (uriRef != NULL)
--- 104,113 ----
   res->next = style->imports;
   style->imports = res;
   style->extrasNr += res->extrasNr;
!  ret = 0;
!     } else {
!  xmlFreeDoc(import);
!  }

  error:
      if (uriRef != NULL)
***************
*** 111,116 ****
--- 116,123 ----
   xmlFree(base);
      if (URI != NULL)
   xmlFree(URI);
+
+     return (ret);
  }

  /**
***************
*** 119,129 ****
   * @template:  the "strip-space" element
   *
   * parse an XSLT stylesheet strip-space element and record
!  * elements needing stripping
   */

! void
  xsltParseStylesheetInclude(xsltStylesheetPtr style, xmlNodePtr cur) {
      xmlDocPtr oldDoc;
      xmlChar *base = NULL;
      xmlChar *uriRef = NULL;
--- 126,138 ----
   * @template:  the "strip-space" element
   *
   * parse an XSLT stylesheet strip-space element and record
!  * elements needing stripping. Returns zero on success, something else
!  * on failure.
   */

! int
  xsltParseStylesheetInclude(xsltStylesheetPtr style, xmlNodePtr cur) {
+     int ret = -1;
      xmlDocPtr oldDoc;
      xmlChar *base = NULL;
      xmlChar *uriRef = NULL;
***************
*** 131,137 ****
      xsltDocumentPtr include;

      if ((cur == NULL) || (style == NULL))
!  return;

      uriRef = xsltGetNsProp(cur, (const xmlChar *)"href", XSLT_NAMESPACE);
      if (uriRef == NULL) {
--- 140,146 ----
      xsltDocumentPtr include;

      if ((cur == NULL) || (style == NULL))
!  return (ret);

      uriRef = xsltGetNsProp(cur, (const xmlChar *)"href", XSLT_NAMESPACE);
      if (uriRef == NULL) {
***************
*** 160,167 ****

      oldDoc = style->doc;
      style->doc = include->doc;
!     xsltParseStylesheetProcess(style, include->doc);
      style->doc = oldDoc;

  error:
      if (uriRef != NULL)
--- 169,181 ----

      oldDoc = style->doc;
      style->doc = include->doc;
!     ret = (int)xsltParseStylesheetProcess(style, include->doc);
      style->doc = oldDoc;
+     if (ret == 0) {
+   ret = -1;
+   goto error;
+  }
+     ret = 0;

  error:
      if (uriRef != NULL)
***************
*** 170,175 ****
--- 184,191 ----
   xmlFree(base);
      if (URI != NULL)
   xmlFree(URI);
+
+     return (ret);
  }

  /**
Index: imports.h
===================================================================
RCS file: /cvs/gnome/libxslt/libxslt/imports.h,v
retrieving revision 1.6
diff -c -r1.6 imports.h
*** imports.h 13 Mar 2002 14:23:40 -0000 1.6
--- imports.h 14 May 2002 17:04:32 -0000
***************
*** 45,53 ****
  /*
   * Module interfaces
   */
! void   xsltParseStylesheetImport(xsltStylesheetPtr style,
          xmlNodePtr cur);
! void   xsltParseStylesheetInclude(xsltStylesheetPtr style,
          xmlNodePtr cur);
  xsltStylesheetPtr xsltNextImport   (xsltStylesheetPtr style);
  int   xsltNeedElemSpaceHandling(xsltTransformContextPtr ctxt);
--- 45,53 ----
  /*
   * Module interfaces
   */
! int   xsltParseStylesheetImport(xsltStylesheetPtr style,
          xmlNodePtr cur);
! int   xsltParseStylesheetInclude(xsltStylesheetPtr style,
          xmlNodePtr cur);
  xsltStylesheetPtr xsltNextImport   (xsltStylesheetPtr style);
  int   xsltNeedElemSpaceHandling(xsltTransformContextPtr ctxt);
Index: xslt.c
===================================================================
RCS file: /cvs/gnome/libxslt/libxslt/xslt.c,v
retrieving revision 1.77
diff -c -r1.77 xslt.c
*** xslt.c 14 Apr 2002 13:35:23 -0000 1.77
--- xslt.c 14 May 2002 17:04:33 -0000
***************
*** 1750,1766 ****
      /*
       * process xsl:import elements
       */
!     while (cur != NULL) {
!  if (IS_BLANK_NODE(cur)) {
!             cur = cur->next;
!      continue;
   }
-  if (IS_XSLT_ELEM(cur) && IS_XSLT_NAME(cur, "import")) {
-      xsltParseStylesheetImport(style, cur);
-  } else
-      break;
-  cur = cur->next;
-     }
      /*
       * process other top-level elements
       */
--- 1750,1767 ----
      /*
       * process xsl:import elements
       */
!  while (cur != NULL) {
!   if (IS_BLANK_NODE(cur)) {
!    cur = cur->next;
!    continue;
!   }
!   if (IS_XSLT_ELEM(cur) && IS_XSLT_NAME(cur, "import")) {
!    if (xsltParseStylesheetImport(style, cur) != 0)
!     style->errors++;
!   } else
!    break;
!   cur = cur->next;
   }
      /*
       * process other top-level elements
       */
***************
*** 1806,1843 ****
   if (IS_XSLT_NAME(cur, "import")) {
       xsltPrintErrorContext(NULL, style, cur);
       xsltGenericError(xsltGenericErrorContext,
!   "xsltParseStylesheetTop: ignoring misplaced import element\n");
       style->errors++;
!         } else if (IS_XSLT_NAME(cur, "include")) {
!      xsltParseStylesheetInclude(style, cur);
!         } else if (IS_XSLT_NAME(cur, "strip-space")) {
       xsltParseStylesheetStripSpace(style, cur);
!         } else if (IS_XSLT_NAME(cur, "preserve-space")) {
       xsltParseStylesheetPreserveSpace(style, cur);
!         } else if (IS_XSLT_NAME(cur, "output")) {
       xsltParseStylesheetOutput(style, cur);
!         } else if (IS_XSLT_NAME(cur, "key")) {
       xsltParseStylesheetKey(style, cur);
!         } else if (IS_XSLT_NAME(cur, "decimal-format")) {
       xsltParseStylesheetDecimalFormat(style, cur);
!         } else if (IS_XSLT_NAME(cur, "attribute-set")) {
       xsltParseStylesheetAttributeSet(style, cur);
!         } else if (IS_XSLT_NAME(cur, "variable")) {
       xsltParseGlobalVariable(style, cur);
!         } else if (IS_XSLT_NAME(cur, "param")) {
       xsltParseGlobalParam(style, cur);
!         } else if (IS_XSLT_NAME(cur, "template")) {
  #ifdef WITH_XSLT_DEBUG_PARSING
       templates++;
  #endif
       xsltParseStylesheetTemplate(style, cur);
!         } else if (IS_XSLT_NAME(cur, "namespace-alias")) {
       xsltNamespaceAlias(style, cur);
   } else {
       xsltPrintErrorContext(NULL, style, cur);
       xsltGenericError(xsltGenericErrorContext,
!   "xsltParseStylesheetTop: ignoring unknown %s element\n",
!                cur->name);
       style->warnings++;
   }
   cur = cur->next;
--- 1807,1845 ----
   if (IS_XSLT_NAME(cur, "import")) {
       xsltPrintErrorContext(NULL, style, cur);
       xsltGenericError(xsltGenericErrorContext,
!    "xsltParseStylesheetTop: ignoring misplaced import element\n");
       style->errors++;
!     } else if (IS_XSLT_NAME(cur, "include")) {
!      if (xsltParseStylesheetInclude(style, cur) != 0)
!    style->errors++;
!     } else if (IS_XSLT_NAME(cur, "strip-space")) {
       xsltParseStylesheetStripSpace(style, cur);
!     } else if (IS_XSLT_NAME(cur, "preserve-space")) {
       xsltParseStylesheetPreserveSpace(style, cur);
!     } else if (IS_XSLT_NAME(cur, "output")) {
       xsltParseStylesheetOutput(style, cur);
!     } else if (IS_XSLT_NAME(cur, "key")) {
       xsltParseStylesheetKey(style, cur);
!     } else if (IS_XSLT_NAME(cur, "decimal-format")) {
       xsltParseStylesheetDecimalFormat(style, cur);
!     } else if (IS_XSLT_NAME(cur, "attribute-set")) {
       xsltParseStylesheetAttributeSet(style, cur);
!     } else if (IS_XSLT_NAME(cur, "variable")) {
       xsltParseGlobalVariable(style, cur);
!     } else if (IS_XSLT_NAME(cur, "param")) {
       xsltParseGlobalParam(style, cur);
!     } else if (IS_XSLT_NAME(cur, "template")) {
  #ifdef WITH_XSLT_DEBUG_PARSING
       templates++;
  #endif
       xsltParseStylesheetTemplate(style, cur);
!     } else if (IS_XSLT_NAME(cur, "namespace-alias")) {
       xsltNamespaceAlias(style, cur);
   } else {
       xsltPrintErrorContext(NULL, style, cur);
       xsltGenericError(xsltGenericErrorContext,
!    "xsltParseStylesheetTop: ignoring unknown %s element\n",
!    cur->name);
       style->warnings++;
   }
   cur = cur->next;
***************
*** 1855,1861 ****
   *
   * parse an XSLT stylesheet adding the associated structures
   *
!  * Returns a new XSLT stylesheet structure.
   */

  xsltStylesheetPtr
--- 1857,1864 ----
   *
   * parse an XSLT stylesheet adding the associated structures
   *
!  * Returns the value of the 'ret' parameter if everything
!  * went right, NULL if something went amiss.
   */

  xsltStylesheetPtr
***************
*** 1877,1884 ****
   xsltPrintErrorContext(NULL, ret, (xmlNodePtr) doc);
          xsltGenericError(xsltGenericErrorContext,
    "xsltParseStylesheetProcess : empty stylesheet\n");
-  ret->doc = NULL;
-  xsltFreeStylesheet(ret);
   return(NULL);
      }
      xsltParseStylesheetExcludePrefix(ret, cur);
--- 1880,1885 ----
***************
*** 1905,1912 ****
       xsltPrintErrorContext(NULL, ret, cur);
       xsltGenericError(xsltGenericErrorContext,
    "xsltParseStylesheetProcess : document is not a stylesheet\n");
-      ret->doc = NULL;
-      xsltFreeStylesheet(ret);
       return(NULL);
   }

--- 1906,1911 ----
***************
*** 1929,1936 ****
    */
   template = xsltNewTemplate();
   if (template == NULL) {
-      ret->doc = NULL;
-      xsltFreeStylesheet(ret);
       return(NULL);
   }
   template->next = ret->templates;
--- 1928,1933 ----
***************
*** 1972,1978 ****

      ret->doc = doc;
      xsltGatherNamespaces(ret);
!     ret = xsltParseStylesheetProcess(ret, doc);

      return(ret);
  }
--- 1969,1979 ----

      ret->doc = doc;
      xsltGatherNamespaces(ret);
!  if (xsltParseStylesheetProcess(ret, doc) == NULL) {
!   ret->doc = NULL;
!   xsltFreeStylesheet(ret);
!   ret = NULL;
!  }

      return(ret);
  }





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