From 8c14927162cf3b4f810683e1c5505e9ef9e1f123 Mon Sep 17 00:00:00 2001
From: Eric Covener <covener@apache.org>
Date: Wed, 1 Jun 2022 12:34:16 +0000
Subject: [PATCH] Merge r1901500 from trunk:

handle large writes in ap_rputs


git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/branches/2.4.x@1901501 13f79535-47bb-0310-9956-ffa450edef68
---
 include/http_protocol.h | 22 +++++++++++++++++++++-
 server/protocol.c       |  3 +++
 2 files changed, 24 insertions(+), 1 deletion(-)

Index: apache2-2.4.29/include/http_protocol.h
===================================================================
--- apache2-2.4.29.orig/include/http_protocol.h
+++ apache2-2.4.29/include/http_protocol.h
@@ -439,7 +439,27 @@ AP_DECLARE(int) ap_rwrite(const void *bu
  */
 static APR_INLINE int ap_rputs(const char *str, request_rec *r)
 {
-    return ap_rwrite(str, (int)strlen(str), r);
+    apr_size_t len;
+
+    len = strlen(str);
+
+    for (;;) {
+        if (len <= INT_MAX) {
+            return ap_rwrite(str, (int)len, r);
+        }
+        else {
+            int rc;
+
+            rc = ap_rwrite(str, INT_MAX, r);
+            if (rc < 0) {
+                return rc;
+            }
+            else {
+                str += INT_MAX;
+                len -= INT_MAX;
+            }
+        }
+    }
 }
 
 /**
Index: apache2-2.4.29/server/protocol.c
===================================================================
--- apache2-2.4.29.orig/server/protocol.c
+++ apache2-2.4.29/server/protocol.c
@@ -2049,6 +2049,9 @@ AP_DECLARE(int) ap_rputc(int c, request_
 
 AP_DECLARE(int) ap_rwrite(const void *buf, int nbyte, request_rec *r)
 {
+    if (nbyte < 0)
+        return -1;
+
     if (r->connection->aborted)
         return -1;
 
