8d2dcd
diff --git a/docs/manual/mod/core.html.en b/docs/manual/mod/core.html.en
8d2dcd
index 20d1e5a..e1ec8d0 100644
8d2dcd
--- a/docs/manual/mod/core.html.en
8d2dcd
+++ b/docs/manual/mod/core.html.en
8d2dcd
@@ -2935,12 +2935,19 @@ from the client
8d2dcd
 Status:Core
8d2dcd
 Module:core
8d2dcd
 
8d2dcd
-    

Limit (in bytes) on maximum size of an XML-based request

8d2dcd
-    body. A value of 0 will disable any checking.

8d2dcd
+    

Limit (in bytes) on the maximum size of an XML-based request

8d2dcd
+    body. A value of 0 will apply a hard limit (depending on
8d2dcd
+    32bit vs 64bit system) allowing for XML escaping within the bounds of
8d2dcd
+    the system addressable memory, but it exists for compatibility only
8d2dcd
+    and is not recommended since it does not account for memory consumed
8d2dcd
+    elsewhere or concurrent requests, which might result in an overall
8d2dcd
+    system out-of-memory.
8d2dcd
+    

8d2dcd
 
8d2dcd
     

Example:

8d2dcd
 
8d2dcd
-    
LimitXMLRequestBody 0
8d2dcd
+    
# Limit of 1 MiB
8d2dcd
+    LimitXMLRequestBody 1073741824
8d2dcd
 
8d2dcd
 
8d2dcd
 
8d2dcd
diff --git a/server/core.c b/server/core.c
8d2dcd
index e32613d..8abfa65 100644
8d2dcd
--- a/server/core.c
8d2dcd
+++ b/server/core.c
8d2dcd
@@ -70,6 +70,8 @@
8d2dcd
 /* LimitXMLRequestBody handling */
8d2dcd
 #define AP_LIMIT_UNSET                  ((long) -1)
8d2dcd
 #define AP_DEFAULT_LIMIT_XML_BODY       ((apr_size_t)1000000)
8d2dcd
+/* Hard limit for ap_escape_html2() */
8d2dcd
+#define AP_MAX_LIMIT_XML_BODY           ((apr_size_t)(APR_SIZE_MAX / 6 - 1))
8d2dcd
 
8d2dcd
 #define AP_MIN_SENDFILE_BYTES           (256)
8d2dcd
 
8d2dcd
@@ -3689,6 +3691,11 @@ static const char *set_limit_xml_req_body(cmd_parms *cmd, void *conf_,
8d2dcd
     if (conf->limit_xml_body < 0)
8d2dcd
         return "LimitXMLRequestBody requires a non-negative integer.";
8d2dcd
 
8d2dcd
+    /* zero is AP_MAX_LIMIT_XML_BODY (implicitly) */
8d2dcd
+    if ((apr_size_t)conf->limit_xml_body > AP_MAX_LIMIT_XML_BODY)
8d2dcd
+        return apr_psprintf(cmd->pool, "LimitXMLRequestBody must not exceed "
8d2dcd
+                            "%" APR_SIZE_T_FMT, AP_MAX_LIMIT_XML_BODY);
8d2dcd
+
8d2dcd
     return NULL;
8d2dcd
 }
8d2dcd
 
8d2dcd
@@ -3777,6 +3784,8 @@ AP_DECLARE(apr_size_t) ap_get_limit_xml_body(const request_rec *r)
8d2dcd
     conf = ap_get_core_module_config(r->per_dir_config);
8d2dcd
     if (conf->limit_xml_body == AP_LIMIT_UNSET)
8d2dcd
         return AP_DEFAULT_LIMIT_XML_BODY;
8d2dcd
+    if (conf->limit_xml_body == 0)
8d2dcd
+        return AP_MAX_LIMIT_XML_BODY;
8d2dcd
 
8d2dcd
     return (apr_size_t)conf->limit_xml_body;
8d2dcd
 }
8d2dcd
diff --git a/server/util.c b/server/util.c
8d2dcd
index 2a5dd04..eefdafa 100644
8d2dcd
--- a/server/util.c
8d2dcd
+++ b/server/util.c
8d2dcd
@@ -2037,11 +2037,14 @@ AP_DECLARE(char *) ap_escape_urlencoded(apr_pool_t *p, const char *buffer)
8d2dcd
 
8d2dcd
 AP_DECLARE(char *) ap_escape_html2(apr_pool_t *p, const char *s, int toasc)
8d2dcd
 {
8d2dcd
-    int i, j;
8d2dcd
+    apr_size_t i, j;
8d2dcd
     char *x;
8d2dcd
 
8d2dcd
     /* first, count the number of extra characters */
8d2dcd
-    for (i = 0, j = 0; s[i] != '\0'; i++)
8d2dcd
+    for (i = 0, j = 0; s[i] != '\0'; i++) {
8d2dcd
+        if (i + j > APR_SIZE_MAX - 6) {
8d2dcd
+            abort();
8d2dcd
+        }
8d2dcd
         if (s[i] == '<' || s[i] == '>')
8d2dcd
             j += 3;
8d2dcd
         else if (s[i] == '&')
8d2dcd
@@ -2050,6 +2053,7 @@ AP_DECLARE(char *) ap_escape_html2(apr_pool_t *p, const char *s, int toasc)
8d2dcd
             j += 5;
8d2dcd
         else if (toasc && !apr_isascii(s[i]))
8d2dcd
             j += 5;
8d2dcd
+    }
8d2dcd
 
8d2dcd
     if (j == 0)
8d2dcd
         return apr_pstrmemdup(p, s, i);
8d2dcd
diff --git a/server/util_xml.c b/server/util_xml.c
8d2dcd
index 4845194..22806fa 100644
8d2dcd
--- a/server/util_xml.c
8d2dcd
+++ b/server/util_xml.c
8d2dcd
@@ -85,7 +85,7 @@ AP_DECLARE(int) ap_xml_parse_input(request_rec * r, apr_xml_doc **pdoc)
8d2dcd
             }
8d2dcd
 
8d2dcd
             total_read += len;
8d2dcd
-            if (limit_xml_body && total_read > limit_xml_body) {
8d2dcd
+            if (total_read > limit_xml_body) {
8d2dcd
                 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00539)
8d2dcd
                               "XML request body is larger than the configured "
8d2dcd
                               "limit of %lu", (unsigned long)limit_xml_body);