Blame SOURCES/libxml2-2.9.13-CVE-2022-40303.patch

ef7ace
From c846986356fc149915a74972bf198abc266bc2c0 Mon Sep 17 00:00:00 2001
ef7ace
From: Nick Wellnhofer <wellnhofer@aevum.de>
ef7ace
Date: Thu, 25 Aug 2022 17:43:08 +0200
ef7ace
Subject: [PATCH] [CVE-2022-40303] Fix integer overflows with XML_PARSE_HUGE
ef7ace
ef7ace
Also impose size limits when XML_PARSE_HUGE is set. Limit size of names
ef7ace
to XML_MAX_TEXT_LENGTH (10 million bytes) and other content to
ef7ace
XML_MAX_HUGE_LENGTH (1 billion bytes).
ef7ace
ef7ace
Move some the length checks to the end of the respective loop to make
ef7ace
them strict.
ef7ace
ef7ace
xmlParseEntityValue didn't have a length limitation at all. But without
ef7ace
XML_PARSE_HUGE, this should eventually trigger an error in xmlGROW.
ef7ace
ef7ace
Thanks to Maddie Stone working with Google Project Zero for the report!
ef7ace
---
ef7ace
 parser.c | 233 +++++++++++++++++++++++++++++--------------------------
ef7ace
 1 file changed, 121 insertions(+), 112 deletions(-)
ef7ace
ef7ace
diff --git a/parser.c b/parser.c
ef7ace
index 93f031be..79479979 100644
ef7ace
--- a/parser.c
ef7ace
+++ b/parser.c
ef7ace
@@ -102,6 +102,8 @@ xmlParseElementEnd(xmlParserCtxtPtr ctxt);
ef7ace
  *									*
ef7ace
  ************************************************************************/
ef7ace
 
ef7ace
+#define XML_MAX_HUGE_LENGTH 1000000000
ef7ace
+
ef7ace
 #define XML_PARSER_BIG_ENTITY 1000
ef7ace
 #define XML_PARSER_LOT_ENTITY 5000
ef7ace
 
ef7ace
@@ -552,7 +554,7 @@ xmlFatalErr(xmlParserCtxtPtr ctxt, xmlParserErrors error, const char *info)
ef7ace
             errmsg = "Malformed declaration expecting version";
ef7ace
             break;
ef7ace
         case XML_ERR_NAME_TOO_LONG:
ef7ace
-            errmsg = "Name too long use XML_PARSE_HUGE option";
ef7ace
+            errmsg = "Name too long";
ef7ace
             break;
ef7ace
 #if 0
ef7ace
         case:
ef7ace
@@ -3202,6 +3204,9 @@ xmlParseNameComplex(xmlParserCtxtPtr ctxt) {
ef7ace
     int len = 0, l;
ef7ace
     int c;
ef7ace
     int count = 0;
ef7ace
+    int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
ef7ace
+                    XML_MAX_TEXT_LENGTH :
ef7ace
+                    XML_MAX_NAME_LENGTH;
ef7ace
 
ef7ace
 #ifdef DEBUG
ef7ace
     nbParseNameComplex++;
ef7ace
@@ -3267,7 +3272,8 @@ xmlParseNameComplex(xmlParserCtxtPtr ctxt) {
ef7ace
                 if (ctxt->instate == XML_PARSER_EOF)
ef7ace
                     return(NULL);
ef7ace
 	    }
ef7ace
-	    len += l;
ef7ace
+            if (len <= INT_MAX - l)
ef7ace
+	        len += l;
ef7ace
 	    NEXTL(l);
ef7ace
 	    c = CUR_CHAR(l);
ef7ace
 	}
ef7ace
@@ -3293,13 +3299,13 @@ xmlParseNameComplex(xmlParserCtxtPtr ctxt) {
ef7ace
                 if (ctxt->instate == XML_PARSER_EOF)
ef7ace
                     return(NULL);
ef7ace
 	    }
ef7ace
-	    len += l;
ef7ace
+            if (len <= INT_MAX - l)
ef7ace
+	        len += l;
ef7ace
 	    NEXTL(l);
ef7ace
 	    c = CUR_CHAR(l);
ef7ace
 	}
ef7ace
     }
ef7ace
-    if ((len > XML_MAX_NAME_LENGTH) &&
ef7ace
-        ((ctxt->options & XML_PARSE_HUGE) == 0)) {
ef7ace
+    if (len > maxLength) {
ef7ace
         xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "Name");
ef7ace
         return(NULL);
ef7ace
     }
ef7ace
@@ -3338,7 +3344,10 @@ const xmlChar *
ef7ace
 xmlParseName(xmlParserCtxtPtr ctxt) {
ef7ace
     const xmlChar *in;
ef7ace
     const xmlChar *ret;
ef7ace
-    int count = 0;
ef7ace
+    size_t count = 0;
ef7ace
+    size_t maxLength = (ctxt->options & XML_PARSE_HUGE) ?
ef7ace
+                       XML_MAX_TEXT_LENGTH :
ef7ace
+                       XML_MAX_NAME_LENGTH;
ef7ace
 
ef7ace
     GROW;
ef7ace
 
ef7ace
@@ -3362,8 +3371,7 @@ xmlParseName(xmlParserCtxtPtr ctxt) {
ef7ace
 	    in++;
ef7ace
 	if ((*in > 0) && (*in < 0x80)) {
ef7ace
 	    count = in - ctxt->input->cur;
ef7ace
-            if ((count > XML_MAX_NAME_LENGTH) &&
ef7ace
-                ((ctxt->options & XML_PARSE_HUGE) == 0)) {
ef7ace
+            if (count > maxLength) {
ef7ace
                 xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "Name");
ef7ace
                 return(NULL);
ef7ace
             }
ef7ace
@@ -3384,6 +3392,9 @@ xmlParseNCNameComplex(xmlParserCtxtPtr ctxt) {
ef7ace
     int len = 0, l;
ef7ace
     int c;
ef7ace
     int count = 0;
ef7ace
+    int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
ef7ace
+                    XML_MAX_TEXT_LENGTH :
ef7ace
+                    XML_MAX_NAME_LENGTH;
ef7ace
     size_t startPosition = 0;
ef7ace
 
ef7ace
 #ifdef DEBUG
ef7ace
@@ -3404,17 +3415,13 @@ xmlParseNCNameComplex(xmlParserCtxtPtr ctxt) {
ef7ace
     while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */
ef7ace
 	   (xmlIsNameChar(ctxt, c) && (c != ':'))) {
ef7ace
 	if (count++ > XML_PARSER_CHUNK_SIZE) {
ef7ace
-            if ((len > XML_MAX_NAME_LENGTH) &&
ef7ace
-                ((ctxt->options & XML_PARSE_HUGE) == 0)) {
ef7ace
-                xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NCName");
ef7ace
-                return(NULL);
ef7ace
-            }
ef7ace
 	    count = 0;
ef7ace
 	    GROW;
ef7ace
             if (ctxt->instate == XML_PARSER_EOF)
ef7ace
                 return(NULL);
ef7ace
 	}
ef7ace
-	len += l;
ef7ace
+        if (len <= INT_MAX - l)
ef7ace
+	    len += l;
ef7ace
 	NEXTL(l);
ef7ace
 	c = CUR_CHAR(l);
ef7ace
 	if (c == 0) {
ef7ace
@@ -3432,8 +3439,7 @@ xmlParseNCNameComplex(xmlParserCtxtPtr ctxt) {
ef7ace
 	    c = CUR_CHAR(l);
ef7ace
 	}
ef7ace
     }
ef7ace
-    if ((len > XML_MAX_NAME_LENGTH) &&
ef7ace
-        ((ctxt->options & XML_PARSE_HUGE) == 0)) {
ef7ace
+    if (len > maxLength) {
ef7ace
         xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NCName");
ef7ace
         return(NULL);
ef7ace
     }
ef7ace
@@ -3459,7 +3465,10 @@ static const xmlChar *
ef7ace
 xmlParseNCName(xmlParserCtxtPtr ctxt) {
ef7ace
     const xmlChar *in, *e;
ef7ace
     const xmlChar *ret;
ef7ace
-    int count = 0;
ef7ace
+    size_t count = 0;
ef7ace
+    size_t maxLength = (ctxt->options & XML_PARSE_HUGE) ?
ef7ace
+                       XML_MAX_TEXT_LENGTH :
ef7ace
+                       XML_MAX_NAME_LENGTH;
ef7ace
 
ef7ace
 #ifdef DEBUG
ef7ace
     nbParseNCName++;
ef7ace
@@ -3484,8 +3493,7 @@ xmlParseNCName(xmlParserCtxtPtr ctxt) {
ef7ace
 	    goto complex;
ef7ace
 	if ((*in > 0) && (*in < 0x80)) {
ef7ace
 	    count = in - ctxt->input->cur;
ef7ace
-            if ((count > XML_MAX_NAME_LENGTH) &&
ef7ace
-                ((ctxt->options & XML_PARSE_HUGE) == 0)) {
ef7ace
+            if (count > maxLength) {
ef7ace
                 xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NCName");
ef7ace
                 return(NULL);
ef7ace
             }
ef7ace
@@ -3567,6 +3575,9 @@ xmlParseStringName(xmlParserCtxtPtr ctxt, const xmlChar** str) {
ef7ace
     const xmlChar *cur = *str;
ef7ace
     int len = 0, l;
ef7ace
     int c;
ef7ace
+    int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
ef7ace
+                    XML_MAX_TEXT_LENGTH :
ef7ace
+                    XML_MAX_NAME_LENGTH;
ef7ace
 
ef7ace
 #ifdef DEBUG
ef7ace
     nbParseStringName++;
ef7ace
@@ -3602,12 +3613,6 @@ xmlParseStringName(xmlParserCtxtPtr ctxt, const xmlChar** str) {
ef7ace
 		if (len + 10 > max) {
ef7ace
 		    xmlChar *tmp;
ef7ace
 
ef7ace
-                    if ((len > XML_MAX_NAME_LENGTH) &&
ef7ace
-                        ((ctxt->options & XML_PARSE_HUGE) == 0)) {
ef7ace
-                        xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NCName");
ef7ace
-			xmlFree(buffer);
ef7ace
-                        return(NULL);
ef7ace
-                    }
ef7ace
 		    max *= 2;
ef7ace
 		    tmp = (xmlChar *) xmlRealloc(buffer,
ef7ace
 			                            max * sizeof(xmlChar));
ef7ace
@@ -3621,14 +3626,18 @@ xmlParseStringName(xmlParserCtxtPtr ctxt, const xmlChar** str) {
ef7ace
 		COPY_BUF(l,buffer,len,c);
ef7ace
 		cur += l;
ef7ace
 		c = CUR_SCHAR(cur, l);
ef7ace
+                if (len > maxLength) {
ef7ace
+                    xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NCName");
ef7ace
+                    xmlFree(buffer);
ef7ace
+                    return(NULL);
ef7ace
+                }
ef7ace
 	    }
ef7ace
 	    buffer[len] = 0;
ef7ace
 	    *str = cur;
ef7ace
 	    return(buffer);
ef7ace
 	}
ef7ace
     }
ef7ace
-    if ((len > XML_MAX_NAME_LENGTH) &&
ef7ace
-        ((ctxt->options & XML_PARSE_HUGE) == 0)) {
ef7ace
+    if (len > maxLength) {
ef7ace
         xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NCName");
ef7ace
         return(NULL);
ef7ace
     }
ef7ace
@@ -3655,6 +3664,9 @@ xmlParseNmtoken(xmlParserCtxtPtr ctxt) {
ef7ace
     int len = 0, l;
ef7ace
     int c;
ef7ace
     int count = 0;
ef7ace
+    int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
ef7ace
+                    XML_MAX_TEXT_LENGTH :
ef7ace
+                    XML_MAX_NAME_LENGTH;
ef7ace
 
ef7ace
 #ifdef DEBUG
ef7ace
     nbParseNmToken++;
ef7ace
@@ -3706,12 +3718,6 @@ xmlParseNmtoken(xmlParserCtxtPtr ctxt) {
ef7ace
 		if (len + 10 > max) {
ef7ace
 		    xmlChar *tmp;
ef7ace
 
ef7ace
-                    if ((max > XML_MAX_NAME_LENGTH) &&
ef7ace
-                        ((ctxt->options & XML_PARSE_HUGE) == 0)) {
ef7ace
-                        xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NmToken");
ef7ace
-                        xmlFree(buffer);
ef7ace
-                        return(NULL);
ef7ace
-                    }
ef7ace
 		    max *= 2;
ef7ace
 		    tmp = (xmlChar *) xmlRealloc(buffer,
ef7ace
 			                            max * sizeof(xmlChar));
ef7ace
@@ -3725,6 +3731,11 @@ xmlParseNmtoken(xmlParserCtxtPtr ctxt) {
ef7ace
 		COPY_BUF(l,buffer,len,c);
ef7ace
 		NEXTL(l);
ef7ace
 		c = CUR_CHAR(l);
ef7ace
+                if (len > maxLength) {
ef7ace
+                    xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NmToken");
ef7ace
+                    xmlFree(buffer);
ef7ace
+                    return(NULL);
ef7ace
+                }
ef7ace
 	    }
ef7ace
 	    buffer[len] = 0;
ef7ace
 	    return(buffer);
ef7ace
@@ -3732,8 +3743,7 @@ xmlParseNmtoken(xmlParserCtxtPtr ctxt) {
ef7ace
     }
ef7ace
     if (len == 0)
ef7ace
         return(NULL);
ef7ace
-    if ((len > XML_MAX_NAME_LENGTH) &&
ef7ace
-        ((ctxt->options & XML_PARSE_HUGE) == 0)) {
ef7ace
+    if (len > maxLength) {
ef7ace
         xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NmToken");
ef7ace
         return(NULL);
ef7ace
     }
ef7ace
@@ -3759,6 +3769,9 @@ xmlParseEntityValue(xmlParserCtxtPtr ctxt, xmlChar **orig) {
ef7ace
     int len = 0;
ef7ace
     int size = XML_PARSER_BUFFER_SIZE;
ef7ace
     int c, l;
ef7ace
+    int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
ef7ace
+                    XML_MAX_HUGE_LENGTH :
ef7ace
+                    XML_MAX_TEXT_LENGTH;
ef7ace
     xmlChar stop;
ef7ace
     xmlChar *ret = NULL;
ef7ace
     const xmlChar *cur = NULL;
ef7ace
@@ -3818,6 +3831,12 @@ xmlParseEntityValue(xmlParserCtxtPtr ctxt, xmlChar **orig) {
ef7ace
 	    GROW;
ef7ace
 	    c = CUR_CHAR(l);
ef7ace
 	}
ef7ace
+
ef7ace
+        if (len > maxLength) {
ef7ace
+            xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_NOT_FINISHED,
ef7ace
+                           "entity value too long\n");
ef7ace
+            goto error;
ef7ace
+        }
ef7ace
     }
ef7ace
     buf[len] = 0;
ef7ace
     if (ctxt->instate == XML_PARSER_EOF)
ef7ace
@@ -3905,6 +3924,9 @@ xmlParseAttValueComplex(xmlParserCtxtPtr ctxt, int *attlen, int normalize) {
ef7ace
     xmlChar *rep = NULL;
ef7ace
     size_t len = 0;
ef7ace
     size_t buf_size = 0;
ef7ace
+    size_t maxLength = (ctxt->options & XML_PARSE_HUGE) ?
ef7ace
+                       XML_MAX_HUGE_LENGTH :
ef7ace
+                       XML_MAX_TEXT_LENGTH;
ef7ace
     int c, l, in_space = 0;
ef7ace
     xmlChar *current = NULL;
ef7ace
     xmlEntityPtr ent;
ef7ace
@@ -3936,16 +3958,6 @@ xmlParseAttValueComplex(xmlParserCtxtPtr ctxt, int *attlen, int normalize) {
ef7ace
     while (((NXT(0) != limit) && /* checked */
ef7ace
             (IS_CHAR(c)) && (c != '<')) &&
ef7ace
             (ctxt->instate != XML_PARSER_EOF)) {
ef7ace
-        /*
ef7ace
-         * Impose a reasonable limit on attribute size, unless XML_PARSE_HUGE
ef7ace
-         * special option is given
ef7ace
-         */
ef7ace
-        if ((len > XML_MAX_TEXT_LENGTH) &&
ef7ace
-            ((ctxt->options & XML_PARSE_HUGE) == 0)) {
ef7ace
-            xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED,
ef7ace
-                           "AttValue length too long\n");
ef7ace
-            goto mem_error;
ef7ace
-        }
ef7ace
 	if (c == '&') {
ef7ace
 	    in_space = 0;
ef7ace
 	    if (NXT(1) == '#') {
ef7ace
@@ -4093,6 +4105,11 @@ xmlParseAttValueComplex(xmlParserCtxtPtr ctxt, int *attlen, int normalize) {
ef7ace
 	}
ef7ace
 	GROW;
ef7ace
 	c = CUR_CHAR(l);
ef7ace
+        if (len > maxLength) {
ef7ace
+            xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED,
ef7ace
+                           "AttValue length too long\n");
ef7ace
+            goto mem_error;
ef7ace
+        }
ef7ace
     }
ef7ace
     if (ctxt->instate == XML_PARSER_EOF)
ef7ace
         goto error;
ef7ace
@@ -4114,16 +4131,6 @@ xmlParseAttValueComplex(xmlParserCtxtPtr ctxt, int *attlen, int normalize) {
ef7ace
     } else
ef7ace
 	NEXT;
ef7ace
 
ef7ace
-    /*
ef7ace
-     * There we potentially risk an overflow, don't allow attribute value of
ef7ace
-     * length more than INT_MAX it is a very reasonable assumption !
ef7ace
-     */
ef7ace
-    if (len >= INT_MAX) {
ef7ace
-        xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED,
ef7ace
-                       "AttValue length too long\n");
ef7ace
-        goto mem_error;
ef7ace
-    }
ef7ace
-
ef7ace
     if (attlen != NULL) *attlen = (int) len;
ef7ace
     return(buf);
ef7ace
 
ef7ace
@@ -4194,6 +4201,9 @@ xmlParseSystemLiteral(xmlParserCtxtPtr ctxt) {
ef7ace
     int len = 0;
ef7ace
     int size = XML_PARSER_BUFFER_SIZE;
ef7ace
     int cur, l;
ef7ace
+    int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
ef7ace
+                    XML_MAX_TEXT_LENGTH :
ef7ace
+                    XML_MAX_NAME_LENGTH;
ef7ace
     xmlChar stop;
ef7ace
     int state = ctxt->instate;
ef7ace
     int count = 0;
ef7ace
@@ -4221,13 +4231,6 @@ xmlParseSystemLiteral(xmlParserCtxtPtr ctxt) {
ef7ace
 	if (len + 5 >= size) {
ef7ace
 	    xmlChar *tmp;
ef7ace
 
ef7ace
-            if ((size > XML_MAX_NAME_LENGTH) &&
ef7ace
-                ((ctxt->options & XML_PARSE_HUGE) == 0)) {
ef7ace
-                xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "SystemLiteral");
ef7ace
-                xmlFree(buf);
ef7ace
-		ctxt->instate = (xmlParserInputState) state;
ef7ace
-                return(NULL);
ef7ace
-            }
ef7ace
 	    size *= 2;
ef7ace
 	    tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
ef7ace
 	    if (tmp == NULL) {
ef7ace
@@ -4256,6 +4259,12 @@ xmlParseSystemLiteral(xmlParserCtxtPtr ctxt) {
ef7ace
 	    SHRINK;
ef7ace
 	    cur = CUR_CHAR(l);
ef7ace
 	}
ef7ace
+        if (len > maxLength) {
ef7ace
+            xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "SystemLiteral");
ef7ace
+            xmlFree(buf);
ef7ace
+            ctxt->instate = (xmlParserInputState) state;
ef7ace
+            return(NULL);
ef7ace
+        }
ef7ace
     }
ef7ace
     buf[len] = 0;
ef7ace
     ctxt->instate = (xmlParserInputState) state;
ef7ace
@@ -4283,6 +4292,9 @@ xmlParsePubidLiteral(xmlParserCtxtPtr ctxt) {
ef7ace
     xmlChar *buf = NULL;
ef7ace
     int len = 0;
ef7ace
     int size = XML_PARSER_BUFFER_SIZE;
ef7ace
+    int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
ef7ace
+                    XML_MAX_TEXT_LENGTH :
ef7ace
+                    XML_MAX_NAME_LENGTH;
ef7ace
     xmlChar cur;
ef7ace
     xmlChar stop;
ef7ace
     int count = 0;
ef7ace
@@ -4310,12 +4322,6 @@ xmlParsePubidLiteral(xmlParserCtxtPtr ctxt) {
ef7ace
 	if (len + 1 >= size) {
ef7ace
 	    xmlChar *tmp;
ef7ace
 
ef7ace
-            if ((size > XML_MAX_NAME_LENGTH) &&
ef7ace
-                ((ctxt->options & XML_PARSE_HUGE) == 0)) {
ef7ace
-                xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "Public ID");
ef7ace
-                xmlFree(buf);
ef7ace
-                return(NULL);
ef7ace
-            }
ef7ace
 	    size *= 2;
ef7ace
 	    tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
ef7ace
 	    if (tmp == NULL) {
ef7ace
@@ -4343,6 +4349,11 @@ xmlParsePubidLiteral(xmlParserCtxtPtr ctxt) {
ef7ace
 	    SHRINK;
ef7ace
 	    cur = CUR;
ef7ace
 	}
ef7ace
+        if (len > maxLength) {
ef7ace
+            xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "Public ID");
ef7ace
+            xmlFree(buf);
ef7ace
+            return(NULL);
ef7ace
+        }
ef7ace
     }
ef7ace
     buf[len] = 0;
ef7ace
     if (cur != stop) {
ef7ace
@@ -4742,6 +4753,9 @@ xmlParseCommentComplex(xmlParserCtxtPtr ctxt, xmlChar *buf,
ef7ace
     int r, rl;
ef7ace
     int cur, l;
ef7ace
     size_t count = 0;
ef7ace
+    size_t maxLength = (ctxt->options & XML_PARSE_HUGE) ?
ef7ace
+                       XML_MAX_HUGE_LENGTH :
ef7ace
+                       XML_MAX_TEXT_LENGTH;
ef7ace
     int inputid;
ef7ace
 
ef7ace
     inputid = ctxt->input->id;
ef7ace
@@ -4787,13 +4801,6 @@ xmlParseCommentComplex(xmlParserCtxtPtr ctxt, xmlChar *buf,
ef7ace
 	if ((r == '-') && (q == '-')) {
ef7ace
 	    xmlFatalErr(ctxt, XML_ERR_HYPHEN_IN_COMMENT, NULL);
ef7ace
 	}
ef7ace
-        if ((len > XML_MAX_TEXT_LENGTH) &&
ef7ace
-            ((ctxt->options & XML_PARSE_HUGE) == 0)) {
ef7ace
-            xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED,
ef7ace
-                         "Comment too big found", NULL);
ef7ace
-            xmlFree (buf);
ef7ace
-            return;
ef7ace
-        }
ef7ace
 	if (len + 5 >= size) {
ef7ace
 	    xmlChar *new_buf;
ef7ace
             size_t new_size;
ef7ace
@@ -4831,6 +4838,13 @@ xmlParseCommentComplex(xmlParserCtxtPtr ctxt, xmlChar *buf,
ef7ace
 	    GROW;
ef7ace
 	    cur = CUR_CHAR(l);
ef7ace
 	}
ef7ace
+
ef7ace
+        if (len > maxLength) {
ef7ace
+            xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED,
ef7ace
+                         "Comment too big found", NULL);
ef7ace
+            xmlFree (buf);
ef7ace
+            return;
ef7ace
+        }
ef7ace
     }
ef7ace
     buf[len] = 0;
ef7ace
     if (cur == 0) {
ef7ace
@@ -4875,6 +4889,9 @@ xmlParseComment(xmlParserCtxtPtr ctxt) {
ef7ace
     xmlChar *buf = NULL;
ef7ace
     size_t size = XML_PARSER_BUFFER_SIZE;
ef7ace
     size_t len = 0;
ef7ace
+    size_t maxLength = (ctxt->options & XML_PARSE_HUGE) ?
ef7ace
+                       XML_MAX_HUGE_LENGTH :
ef7ace
+                       XML_MAX_TEXT_LENGTH;
ef7ace
     xmlParserInputState state;
ef7ace
     const xmlChar *in;
ef7ace
     size_t nbchar = 0;
ef7ace
@@ -4958,8 +4975,7 @@ get_more:
ef7ace
 		buf[len] = 0;
ef7ace
 	    }
ef7ace
 	}
ef7ace
-        if ((len > XML_MAX_TEXT_LENGTH) &&
ef7ace
-            ((ctxt->options & XML_PARSE_HUGE) == 0)) {
ef7ace
+        if (len > maxLength) {
ef7ace
             xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED,
ef7ace
                          "Comment too big found", NULL);
ef7ace
             xmlFree (buf);
ef7ace
@@ -5159,6 +5175,9 @@ xmlParsePI(xmlParserCtxtPtr ctxt) {
ef7ace
     xmlChar *buf = NULL;
ef7ace
     size_t len = 0;
ef7ace
     size_t size = XML_PARSER_BUFFER_SIZE;
ef7ace
+    size_t maxLength = (ctxt->options & XML_PARSE_HUGE) ?
ef7ace
+                       XML_MAX_HUGE_LENGTH :
ef7ace
+                       XML_MAX_TEXT_LENGTH;
ef7ace
     int cur, l;
ef7ace
     const xmlChar *target;
ef7ace
     xmlParserInputState state;
ef7ace
@@ -5234,14 +5253,6 @@ xmlParsePI(xmlParserCtxtPtr ctxt) {
ef7ace
                         return;
ef7ace
                     }
ef7ace
 		    count = 0;
ef7ace
-                    if ((len > XML_MAX_TEXT_LENGTH) &&
ef7ace
-                        ((ctxt->options & XML_PARSE_HUGE) == 0)) {
ef7ace
-                        xmlFatalErrMsgStr(ctxt, XML_ERR_PI_NOT_FINISHED,
ef7ace
-                                          "PI %s too big found", target);
ef7ace
-                        xmlFree(buf);
ef7ace
-                        ctxt->instate = state;
ef7ace
-                        return;
ef7ace
-                    }
ef7ace
 		}
ef7ace
 		COPY_BUF(l,buf,len,cur);
ef7ace
 		NEXTL(l);
ef7ace
@@ -5251,15 +5262,14 @@ xmlParsePI(xmlParserCtxtPtr ctxt) {
ef7ace
 		    GROW;
ef7ace
 		    cur = CUR_CHAR(l);
ef7ace
 		}
ef7ace
+                if (len > maxLength) {
ef7ace
+                    xmlFatalErrMsgStr(ctxt, XML_ERR_PI_NOT_FINISHED,
ef7ace
+                                      "PI %s too big found", target);
ef7ace
+                    xmlFree(buf);
ef7ace
+                    ctxt->instate = state;
ef7ace
+                    return;
ef7ace
+                }
ef7ace
 	    }
ef7ace
-            if ((len > XML_MAX_TEXT_LENGTH) &&
ef7ace
-                ((ctxt->options & XML_PARSE_HUGE) == 0)) {
ef7ace
-                xmlFatalErrMsgStr(ctxt, XML_ERR_PI_NOT_FINISHED,
ef7ace
-                                  "PI %s too big found", target);
ef7ace
-                xmlFree(buf);
ef7ace
-                ctxt->instate = state;
ef7ace
-                return;
ef7ace
-            }
ef7ace
 	    buf[len] = 0;
ef7ace
 	    if (cur != '?') {
ef7ace
 		xmlFatalErrMsgStr(ctxt, XML_ERR_PI_NOT_FINISHED,
ef7ace
@@ -8954,6 +8964,9 @@ xmlParseAttValueInternal(xmlParserCtxtPtr ctxt, int *len, int *alloc,
ef7ace
     const xmlChar *in = NULL, *start, *end, *last;
ef7ace
     xmlChar *ret = NULL;
ef7ace
     int line, col;
ef7ace
+    int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
ef7ace
+                    XML_MAX_HUGE_LENGTH :
ef7ace
+                    XML_MAX_TEXT_LENGTH;
ef7ace
 
ef7ace
     GROW;
ef7ace
     in = (xmlChar *) CUR_PTR;
ef7ace
@@ -8993,8 +9006,7 @@ xmlParseAttValueInternal(xmlParserCtxtPtr ctxt, int *len, int *alloc,
ef7ace
 	    start = in;
ef7ace
 	    if (in >= end) {
ef7ace
                 GROW_PARSE_ATT_VALUE_INTERNAL(ctxt, in, start, end)
ef7ace
-                if (((in - start) > XML_MAX_TEXT_LENGTH) &&
ef7ace
-                    ((ctxt->options & XML_PARSE_HUGE) == 0)) {
ef7ace
+                if ((in - start) > maxLength) {
ef7ace
                     xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED,
ef7ace
                                    "AttValue length too long\n");
ef7ace
                     return(NULL);
ef7ace
@@ -9007,8 +9019,7 @@ xmlParseAttValueInternal(xmlParserCtxtPtr ctxt, int *len, int *alloc,
ef7ace
 	    if ((*in++ == 0x20) && (*in == 0x20)) break;
ef7ace
 	    if (in >= end) {
ef7ace
                 GROW_PARSE_ATT_VALUE_INTERNAL(ctxt, in, start, end)
ef7ace
-                if (((in - start) > XML_MAX_TEXT_LENGTH) &&
ef7ace
-                    ((ctxt->options & XML_PARSE_HUGE) == 0)) {
ef7ace
+                if ((in - start) > maxLength) {
ef7ace
                     xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED,
ef7ace
                                    "AttValue length too long\n");
ef7ace
                     return(NULL);
ef7ace
@@ -9041,16 +9052,14 @@ xmlParseAttValueInternal(xmlParserCtxtPtr ctxt, int *len, int *alloc,
ef7ace
 		    last = last + delta;
ef7ace
 		}
ef7ace
 		end = ctxt->input->end;
ef7ace
-                if (((in - start) > XML_MAX_TEXT_LENGTH) &&
ef7ace
-                    ((ctxt->options & XML_PARSE_HUGE) == 0)) {
ef7ace
+                if ((in - start) > maxLength) {
ef7ace
                     xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED,
ef7ace
                                    "AttValue length too long\n");
ef7ace
                     return(NULL);
ef7ace
                 }
ef7ace
 	    }
ef7ace
 	}
ef7ace
-        if (((in - start) > XML_MAX_TEXT_LENGTH) &&
ef7ace
-            ((ctxt->options & XML_PARSE_HUGE) == 0)) {
ef7ace
+        if ((in - start) > maxLength) {
ef7ace
             xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED,
ef7ace
                            "AttValue length too long\n");
ef7ace
             return(NULL);
ef7ace
@@ -9063,8 +9072,7 @@ xmlParseAttValueInternal(xmlParserCtxtPtr ctxt, int *len, int *alloc,
ef7ace
 	    col++;
ef7ace
 	    if (in >= end) {
ef7ace
                 GROW_PARSE_ATT_VALUE_INTERNAL(ctxt, in, start, end)
ef7ace
-                if (((in - start) > XML_MAX_TEXT_LENGTH) &&
ef7ace
-                    ((ctxt->options & XML_PARSE_HUGE) == 0)) {
ef7ace
+                if ((in - start) > maxLength) {
ef7ace
                     xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED,
ef7ace
                                    "AttValue length too long\n");
ef7ace
                     return(NULL);
ef7ace
@@ -9072,8 +9080,7 @@ xmlParseAttValueInternal(xmlParserCtxtPtr ctxt, int *len, int *alloc,
ef7ace
 	    }
ef7ace
 	}
ef7ace
 	last = in;
ef7ace
-        if (((in - start) > XML_MAX_TEXT_LENGTH) &&
ef7ace
-            ((ctxt->options & XML_PARSE_HUGE) == 0)) {
ef7ace
+        if ((in - start) > maxLength) {
ef7ace
             xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED,
ef7ace
                            "AttValue length too long\n");
ef7ace
             return(NULL);
ef7ace
@@ -9763,6 +9770,9 @@ xmlParseCDSect(xmlParserCtxtPtr ctxt) {
ef7ace
     int	s, sl;
ef7ace
     int cur, l;
ef7ace
     int count = 0;
ef7ace
+    int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
ef7ace
+                    XML_MAX_HUGE_LENGTH :
ef7ace
+                    XML_MAX_TEXT_LENGTH;
ef7ace
 
ef7ace
     /* Check 2.6.0 was NXT(0) not RAW */
ef7ace
     if (CMP9(CUR_PTR, '<', '!', '[', 'C', 'D', 'A', 'T', 'A', '[')) {
ef7ace
@@ -9796,13 +9806,6 @@ xmlParseCDSect(xmlParserCtxtPtr ctxt) {
ef7ace
 	if (len + 5 >= size) {
ef7ace
 	    xmlChar *tmp;
ef7ace
 
ef7ace
-            if ((size > XML_MAX_TEXT_LENGTH) &&
ef7ace
-                ((ctxt->options & XML_PARSE_HUGE) == 0)) {
ef7ace
-                xmlFatalErrMsgStr(ctxt, XML_ERR_CDATA_NOT_FINISHED,
ef7ace
-                             "CData section too big found", NULL);
ef7ace
-                xmlFree (buf);
ef7ace
-                return;
ef7ace
-            }
ef7ace
 	    tmp = (xmlChar *) xmlRealloc(buf, size * 2 * sizeof(xmlChar));
ef7ace
 	    if (tmp == NULL) {
ef7ace
 	        xmlFree(buf);
ef7ace
@@ -9829,6 +9832,12 @@ xmlParseCDSect(xmlParserCtxtPtr ctxt) {
ef7ace
 	}
ef7ace
 	NEXTL(l);
ef7ace
 	cur = CUR_CHAR(l);
ef7ace
+        if (len > maxLength) {
ef7ace
+            xmlFatalErrMsg(ctxt, XML_ERR_CDATA_NOT_FINISHED,
ef7ace
+                           "CData section too big found\n");
ef7ace
+            xmlFree(buf);
ef7ace
+            return;
ef7ace
+        }
ef7ace
     }
ef7ace
     buf[len] = 0;
ef7ace
     ctxt->instate = XML_PARSER_CONTENT;
ef7ace
-- 
ef7ace
GitLab
ef7ace