This is an automated email from the ASF dual-hosted git repository. markt pushed a commit to branch 10.1.x in repository https://gitbox.apache.org/repos/asf/tomcat.git
commit 152c5259ef48a0ad768193d90c974125c59ac089 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 b5e6a38088..2de7d243af 100644 --- a/java/org/apache/catalina/connector/InputBuffer.java +++ b/java/org/apache/catalina/connector/InputBuffer.java @@ -60,6 +60,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 @@ -76,8 +78,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; @@ -151,8 +156,8 @@ public class InputBuffer extends Reader implements ByteChunk.ByteInputChannel, A public InputBuffer(int size) { 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; @@ -191,7 +196,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 f6e77ef9bf..4424d5e404 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -133,6 +133,11 @@ <pr>843</pr>: Fix off by one validation logic for partial PUT ranges and associated test case. Submitted by Chenjp. (remm) </fix> + <scode> + Replace the unused buffer in + <code>org.apache.catalina.connector.InputBuffer</code> with a static, + zero length buffer. (markt) + </scode> </changelog> </subsection> <subsection name="Coyote"> --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org