remove the warning for startDocument(), as it is used by glade (or

* legacy.c: remove the warning for startDocument(), as it is used by
  glade (or glade-python)
* parser.c relaxng.c xmlschemastypes.c: fixed an assorted set of
  invalid accesses found by running some Python based regression
  tests under valgrind. There is still a few leaks reported by the
  relaxng regressions which need some attention.
* doc/Makefile.am: fixed a make install problem c.f. #124539
* include/libxml/parserInternals.h: addition of xmlParserMaxDepth
  patch from crutcher
Daniel
diff --git a/ChangeLog b/ChangeLog
index dd67a86..4994201 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+Fri Oct 17 14:38:54 CEST 2003 Daniel Veillard <daniel@veillard.com>
+
+	* legacy.c: remove the warning for startDocument(), as it is used by
+	  glade (or glade-python)
+	* parser.c relaxng.c xmlschemastypes.c: fixed an assorted set of
+	  invalid accesses found by running some Python based regression
+	  tests under valgrind. There is still a few leaks reported by the
+	  relaxng regressions which need some attention.
+	* doc/Makefile.am: fixed a make install problem c.f. #124539
+	* include/libxml/parserInternals.h: addition of xmlParserMaxDepth
+	  patch from crutcher
+
 Wed Oct 15 12:47:33 CEST 2003 Daniel Veillard <daniel@veillard.com>
 
 	* parser.c: Marc Liyanage pointed out that xmlCleanupParser()
diff --git a/doc/Makefile.am b/doc/Makefile.am
index 3b5ec2f..d018f39 100644
--- a/doc/Makefile.am
+++ b/doc/Makefile.am
@@ -100,8 +100,10 @@
 install-data-local: 
 	$(mkinstalldirs) $(DESTDIR)$(TARGET_DIR)
 	-@INSTALL@ -m 0644 $(srcdir)/xml.html $(srcdir)/encoding.html $(srcdir)/FAQ.html $(srcdir)/structure.gif $(srcdir)/DOM.gif $(srcdir)/smallfootonly.gif $(srcdir)/redhat.gif $(srcdir)/libxml.gif $(srcdir)/w3c.png $(srcdir)/Libxml2-Logo-180x168.gif $(srcdir)/Libxml2-Logo-90x34.gif $(DESTDIR)$(TARGET_DIR)
-	-@INSTALL@ -m 0644 $(srcdir)/html/*.html $(DESTDIR)$(TARGET_DIR)
-	-@INSTALL@ -m 0644 $(srcdir)/html/index.sgml $(DESTDIR)$(TARGET_DIR)
+	$(mkinstalldirs) $(DESTDIR)$(TARGET_DIR)/html
+	-@INSTALL@ -m 0644 $(srcdir)/html/*.html $(DESTDIR)$(TARGET_DIR)/html
+	-@INSTALL@ -m 0644 $(srcdir)/html/*.png $(DESTDIR)$(TARGET_DIR)/html
+	-@INSTALL@ -m 0644 $(srcdir)/html/index.sgml $(DESTDIR)$(TARGET_DIR)/html
 	-(cd $(DESTDIR); gtkdoc-fixxref --module=libxml --html-dir=$(HTML_DIR))
 	-@(tar cf - tutorial | (cd $(DESTDIR)$(TARGET_DIR) && tar xvf -))
 
diff --git a/include/libxml/parserInternals.h b/include/libxml/parserInternals.h
index 2956064..e66863b 100644
--- a/include/libxml/parserInternals.h
+++ b/include/libxml/parserInternals.h
@@ -19,6 +19,15 @@
 extern "C" {
 #endif
 
+/**
+ * xmlParserMaxDepth:
+ *
+ * arbitrary depth limit for the XML documents that we allow to 
+ * process. This is not a limitation of the parser but a safety 
+ * boundary feature.
+ */
+XMLPUBVAR unsigned int xmlParserMaxDepth;
+
  /**
   * XML_MAX_NAMELEN:
   *
diff --git a/legacy.c b/legacy.c
index 6f86b95..5b635ff 100644
--- a/legacy.c
+++ b/legacy.c
@@ -1084,7 +1084,8 @@
 void
 startDocument(void *ctx)
 {
-    DEPRECATED("startDocument")
+   /* don't be too painful for glade users */
+   /*  DEPRECATED("startDocument") */
         xmlSAX2StartDocument(ctx);
 }
 
diff --git a/parser.c b/parser.c
index 82e5cce..9c51cdc 100644
--- a/parser.c
+++ b/parser.c
@@ -78,13 +78,13 @@
 #endif
 
 /**
- * MAX_DEPTH:
+ * xmlParserMaxDepth:
  *
  * arbitrary depth limit for the XML documents that we allow to 
  * process. This is not a limitation of the parser but a safety 
  * boundary feature.
  */
-#define MAX_DEPTH 1024
+unsigned int xmlParserMaxDepth = 1024;
 
 #define SAX2 1
 
@@ -972,15 +972,13 @@
             return (0);
         }
     }
-#ifdef MAX_DEPTH
-    if (ctxt->nodeNr > MAX_DEPTH) {
+    if (((unsigned int) ctxt->nodeNr) > xmlParserMaxDepth) {
 	xmlFatalErrMsgInt(ctxt, XML_ERR_INTERNAL_ERROR,
-		 "Excessive depth in document: change MAX_DEPTH = %d\n",
-			  MAX_DEPTH);
+		 "Excessive depth in document: change xmlParserMaxDepth = %d\n",
+			  xmlParserMaxDepth);
 	ctxt->instate = XML_PARSER_EOF;
 	return(0);
     }
-#endif
     ctxt->nodeTab[ctxt->nodeNr] = value;
     ctxt->node = value;
     return (ctxt->nodeNr++);
@@ -3101,6 +3099,8 @@
 	    }
 	    if (name != NULL)
 		xmlFree(name);
+	    if (*cur == 0)
+	        break;
 	}
 	cur++;
     }
@@ -3810,10 +3810,16 @@
 	return;
     }
     q = CUR_CHAR(ql);
+    if (q == 0)
+        goto not_terminated;
     NEXTL(ql);
     r = CUR_CHAR(rl);
+    if (r == 0)
+        goto not_terminated;
     NEXTL(rl);
     cur = CUR_CHAR(l);
+    if (cur == 0)
+        goto not_terminated;
     len = 0;
     while (xmlIsChar(cur) && /* checked */
            ((cur != '>') ||
@@ -3866,6 +3872,11 @@
 	xmlFree(buf);
     }
     ctxt->instate = state;
+    return;
+not_terminated:
+    xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED,
+			 "Comment not terminated\n", NULL);
+    xmlFree(buf);
 }
 
 /**
@@ -5763,7 +5774,7 @@
 		/*
 		 * Check that this entity is well formed
 		 */
-		if ((value != NULL) &&
+		if ((value != NULL) && (value[0] != 0) &&
 		    (value[1] == 0) && (value[0] == '<') &&
 		    (xmlStrEqual(ent->name, BAD_CAST "lt"))) {
 		    /*
@@ -8776,7 +8787,8 @@
     if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
         ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator);
 
-    if (ctxt->encoding == (const xmlChar *)XML_CHAR_ENCODING_NONE) {
+    if ((ctxt->encoding == (const xmlChar *)XML_CHAR_ENCODING_NONE) &&
+        ((ctxt->input->end - ctxt->input->cur) >= 4)) {
 	/* 
 	 * Get the 4 first bytes and decode the charset
 	 * if enc != XML_CHAR_ENCODING_NONE
@@ -8786,7 +8798,7 @@
 	start[1] = NXT(1);
 	start[2] = NXT(2);
 	start[3] = NXT(3);
-	enc = xmlDetectCharEncoding(start, 4);
+	enc = xmlDetectCharEncoding(&start[0], 4);
 	if (enc != XML_CHAR_ENCODING_NONE) {
 	    xmlSwitchEncoding(ctxt, enc);
 	}
@@ -8938,13 +8950,15 @@
      * if enc != XML_CHAR_ENCODING_NONE
      * plug some encoding conversion routines.
      */
-    start[0] = RAW;
-    start[1] = NXT(1);
-    start[2] = NXT(2);
-    start[3] = NXT(3);
-    enc = xmlDetectCharEncoding(start, 4);
-    if (enc != XML_CHAR_ENCODING_NONE) {
-        xmlSwitchEncoding(ctxt, enc);
+    if ((ctxt->input->end - ctxt->input->cur) >= 4) {
+	start[0] = RAW;
+	start[1] = NXT(1);
+	start[2] = NXT(2);
+	start[3] = NXT(3);
+	enc = xmlDetectCharEncoding(start, 4);
+	if (enc != XML_CHAR_ENCODING_NONE) {
+	    xmlSwitchEncoding(ctxt, enc);
+	}
     }
 
 
@@ -10314,7 +10328,8 @@
     ctxt->myDoc->extSubset = xmlNewDtd(ctxt->myDoc, BAD_CAST "none",
 	                               BAD_CAST "none", BAD_CAST "none");
 
-    if (enc == XML_CHAR_ENCODING_NONE) {
+    if ((enc == XML_CHAR_ENCODING_NONE) &&
+        ((ctxt->input->end - ctxt->input->cur) >= 4)) {
 	/* 
 	 * Get the 4 first bytes and decode the charset
 	 * if enc != XML_CHAR_ENCODING_NONE
@@ -10410,8 +10425,10 @@
      * plug some encoding conversion routines here.
      */
     xmlPushInput(ctxt, input);
-    enc = xmlDetectCharEncoding(ctxt->input->cur, 4);
-    xmlSwitchEncoding(ctxt, enc);
+    if ((ctxt->input->end - ctxt->input->cur) >= 4) {
+	enc = xmlDetectCharEncoding(ctxt->input->cur, 4);
+	xmlSwitchEncoding(ctxt, enc);
+    }
 
     if (input->filename == NULL)
 	input->filename = (char *) xmlCanonicPath(SystemID);
@@ -10560,13 +10577,15 @@
      * plug some encoding conversion routines.
      */
     GROW
-    start[0] = RAW;
-    start[1] = NXT(1);
-    start[2] = NXT(2);
-    start[3] = NXT(3);
-    enc = xmlDetectCharEncoding(start, 4);
-    if (enc != XML_CHAR_ENCODING_NONE) {
-        xmlSwitchEncoding(ctxt, enc);
+    if ((ctxt->input->end - ctxt->input->cur) >= 4) {
+	start[0] = RAW;
+	start[1] = NXT(1);
+	start[2] = NXT(2);
+	start[3] = NXT(3);
+	enc = xmlDetectCharEncoding(start, 4);
+	if (enc != XML_CHAR_ENCODING_NONE) {
+	    xmlSwitchEncoding(ctxt, enc);
+	}
     }
 
     /*
@@ -10756,13 +10775,15 @@
      * plug some encoding conversion routines.
      */
     GROW;
-    start[0] = RAW;
-    start[1] = NXT(1);
-    start[2] = NXT(2);
-    start[3] = NXT(3);
-    enc = xmlDetectCharEncoding(start, 4);
-    if (enc != XML_CHAR_ENCODING_NONE) {
-        xmlSwitchEncoding(ctxt, enc);
+    if ((ctxt->input->end - ctxt->input->cur) >= 4) {
+	start[0] = RAW;
+	start[1] = NXT(1);
+	start[2] = NXT(2);
+	start[3] = NXT(3);
+	enc = xmlDetectCharEncoding(start, 4);
+	if (enc != XML_CHAR_ENCODING_NONE) {
+	    xmlSwitchEncoding(ctxt, enc);
+	}
     }
 
     /*
diff --git a/relaxng.c b/relaxng.c
index 0330bec..f3ce7f5 100644
--- a/relaxng.c
+++ b/relaxng.c
@@ -6696,11 +6696,11 @@
             /* don't try to normalize the inner spaces */
             while (IS_BLANK(*cur))
                 cur++;
-            *start++ = *cur++;
             if (*cur == 0) {
                 *start = 0;
                 return;
             }
+            *start++ = *cur++;
         } while (1);
     }
 }
diff --git a/xmlschemastypes.c b/xmlschemastypes.c
index 5920aed..4830924 100644
--- a/xmlschemastypes.c
+++ b/xmlschemastypes.c
@@ -2857,21 +2857,24 @@
 		    xmlSchemaFreeValue(q1);
                     return -1;
 		} else {
+		    int ret = 0;
                     /* normalize y - 14:00 */
                     q2 = xmlSchemaDateNormalize(y, -(14 * SECS_PER_HOUR));
                     q2d = _xmlSchemaDateCastYMToDays(q2) + q2->value.date.day;
-		    xmlSchemaFreeValue(p1);
-		    xmlSchemaFreeValue(q1);
-		    xmlSchemaFreeValue(q2);
                     if (p1d > q2d)
-                        return 1;
+                        ret = 1;
                     else if (p1d == q2d) {
                         sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q2);
                         if (sec > 0.0)
-                            return 1;
+                            ret = 1;
                         else
-                            return 2; /* indeterminate */
+                            ret = 2; /* indeterminate */
                     }
+		    xmlSchemaFreeValue(p1);
+		    xmlSchemaFreeValue(q1);
+		    xmlSchemaFreeValue(q2);
+		    if (ret != 0)
+		        return(ret);
                 }
             } else {
 		xmlSchemaFreeValue(p1);
@@ -2899,28 +2902,25 @@
 		xmlSchemaFreeValue(q1);
                 return -1;
 	    } else {
+	        int ret = 0;
                 /* normalize x + 14:00 */
                 p2 = xmlSchemaDateNormalize(x, (14 * SECS_PER_HOUR));
                 p2d = _xmlSchemaDateCastYMToDays(p2) + p2->value.date.day;
 
                 if (p2d > q1d) {
-		    xmlSchemaFreeValue(p1);
-		    xmlSchemaFreeValue(q1);
-		    xmlSchemaFreeValue(p2);
-                    return 1;
+                    ret = 1;
 		} else if (p2d == q1d) {
                     sec = TIME_TO_NUMBER(p2) - TIME_TO_NUMBER(q1);
-		    xmlSchemaFreeValue(p1);
-		    xmlSchemaFreeValue(q1);
-		    xmlSchemaFreeValue(p2);
                     if (sec > 0.0)
-                        return 1;
+                        ret = 1;
                     else
-                        return 2; /* indeterminate */
+                        ret = 2; /* indeterminate */
                 }
 		xmlSchemaFreeValue(p1);
 		xmlSchemaFreeValue(q1);
 		xmlSchemaFreeValue(p2);
+		if (ret != 0)
+		    return(ret);
             }
 	} else {
 	    xmlSchemaFreeValue(p1);
@@ -2932,6 +2932,7 @@
      * if the same type then calculate the difference
      */
     if (x->type == y->type) {
+        int ret = 0;
         q1 = xmlSchemaDateNormalize(y, 0);
         q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
 
@@ -2939,26 +2940,22 @@
         p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
 
         if (p1d < q1d) {
-	    xmlSchemaFreeValue(p1);
-	    xmlSchemaFreeValue(q1);
-            return -1;
+            ret = -1;
 	} else if (p1d > q1d) {
-	    xmlSchemaFreeValue(p1);
-	    xmlSchemaFreeValue(q1);
-            return 1;
+            ret = 1;
 	} else {
             double sec;
 
             sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
-	    xmlSchemaFreeValue(p1);
-	    xmlSchemaFreeValue(q1);
             if (sec < 0.0)
-                return -1;
+                ret = -1;
             else if (sec > 0.0)
-                return 1;
+                ret = 1;
             
         }
-        return 0;
+	xmlSchemaFreeValue(p1);
+	xmlSchemaFreeValue(q1);
+        return(ret);
     }
 
     switch (x->type) {