This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch 11.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git

commit 3f3998dc76324369701a85198d0685c20f0ca487
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Thu Jan 9 15:19:00 2025 +0000

    Replace the unused buffer with a zero length, static instance.
---
 java/org/apache/catalina/connector/InputBuffer.java | 19 ++++++++++++++-----
 webapps/docs/changelog.xml                          |  5 +++++
 2 files changed, 19 insertions(+), 5 deletions(-)

diff --git a/java/org/apache/catalina/connector/InputBuffer.java 
b/java/org/apache/catalina/connector/InputBuffer.java
index 45d64bc8e5..6d5db65ec1 100644
--- a/java/org/apache/catalina/connector/InputBuffer.java
+++ b/java/org/apache/catalina/connector/InputBuffer.java
@@ -56,6 +56,8 @@ public class InputBuffer extends Reader implements 
ByteChunk.ByteInputChannel, A
 
     private static final Log log = LogFactory.getLog(InputBuffer.class);
 
+    private static final ByteBuffer EMPTY_BUFFER = ByteBuffer.allocate(0);
+
     public static final int DEFAULT_BUFFER_SIZE = 8 * 1024;
 
     // The buffer can be used for byte[] and char[] reading
@@ -72,8 +74,11 @@ public class InputBuffer extends Reader implements 
ByteChunk.ByteInputChannel, A
 
     // ----------------------------------------------------- Instance Variables
 
-    /**
-     * The byte buffer.
+    /*
+     * The byte buffer. Data is always injected into this class by calling 
{@link #setByteBuffer(ByteBuffer)} rather
+     * than copying data into any existing buffer. It is initialised to an 
empty buffer as there are code paths that
+     * access the buffer when it is expected to be empty and an empty buffer 
gives cleaner code than lots of null
+     * checks.
      */
     private ByteBuffer bb;
 
@@ -147,8 +152,8 @@ public class InputBuffer extends Reader implements 
ByteChunk.ByteInputChannel, A
      */
     public InputBuffer(int size, org.apache.coyote.Request coyoteRequest) {
         this.size = size;
-        bb = ByteBuffer.allocate(size);
-        clear(bb);
+        // Will be replaced when there is data to read so initialise to empty 
buffer.
+        bb = EMPTY_BUFFER;
         cb = CharBuffer.allocate(size);
         clear(cb);
         readLimit = size;
@@ -175,7 +180,11 @@ public class InputBuffer extends Reader implements 
ByteChunk.ByteInputChannel, A
         }
         readLimit = size;
         markPos = -1;
-        clear(bb);
+        /*
+         * This buffer will have been replaced if there was data to read so 
re-initialise to an empty buffer to clear
+         * any reference to an injected buffer.
+         */
+        bb = EMPTY_BUFFER;
         closed = false;
 
         if (conv != null) {
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index 3d12fb0c8e..5c211b1239 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -148,6 +148,11 @@
         Refactor the <code>SavedRequestInputFilter</code> so the buffered data
         is used directly rather than copied. (markt)
       </scode>
+      <scode>
+        Replace the unused buffer in
+        <code>org.apache.catalina.connector.InputBuffer</code> with a static,
+        zero length buffer. (markt)
+      </scode>
     </changelog>
   </subsection>
 </section>


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to