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

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


The following commit(s) were added to refs/heads/9.0.x by this push:
     new 7cd2947b83 Avoid unchecked use of the backing array
7cd2947b83 is described below

commit 7cd2947b83e75600138d2ee3a358834ece48fd9b
Author: remm <r...@apache.org>
AuthorDate: Fri Apr 21 09:52:54 2023 +0200

    Avoid unchecked use of the backing array
    
    This comes from user code and can happen with a direct (bad idea ...) or
    read only buffer. This will cause inefficient byte copying.
    Also review all other uses of .array() in Tomcat, which all seem safe.
---
 java/org/apache/tomcat/websocket/PerMessageDeflate.java | 12 +++++++++---
 webapps/docs/changelog.xml                              |  8 ++++++++
 2 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/java/org/apache/tomcat/websocket/PerMessageDeflate.java 
b/java/org/apache/tomcat/websocket/PerMessageDeflate.java
index 4bc97b8199..a7a9fb4524 100644
--- a/java/org/apache/tomcat/websocket/PerMessageDeflate.java
+++ b/java/org/apache/tomcat/websocket/PerMessageDeflate.java
@@ -329,9 +329,15 @@ public class PerMessageDeflate implements Transformation {
                 ByteBuffer uncompressedPayload = uncompressedPart.getPayload();
                 SendHandler uncompressedIntermediateHandler = 
uncompressedPart.getIntermediateHandler();
 
-                deflater.setInput(uncompressedPayload.array(),
-                        uncompressedPayload.arrayOffset() + 
uncompressedPayload.position(),
-                        uncompressedPayload.remaining());
+                if (uncompressedPayload.hasArray()) {
+                    deflater.setInput(uncompressedPayload.array(),
+                            uncompressedPayload.arrayOffset() + 
uncompressedPayload.position(),
+                            uncompressedPayload.remaining());
+                } else {
+                    byte[] bytes = new byte[uncompressedPayload.remaining()];
+                    uncompressedPayload.get(bytes);
+                    deflater.setInput(bytes, 0, bytes.length);
+                }
 
                 int flush = (uncompressedPart.isFin() ? Deflater.SYNC_FLUSH : 
Deflater.NO_FLUSH);
                 boolean deflateRequired = true;
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index 57ad80dd43..c0c2383e6c 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -121,6 +121,14 @@
       </add>
     </changelog>
   </subsection>
+  <subsection name="WebSocket">
+    <changelog>
+      <fix>
+        <bug>66575</bug>: Avoid unchecked use of the backing array of a
+        buffer provided by the user in the compression transformation. (remm)
+      </fix>
+    </changelog>
+  </subsection>
 </section>
 <section name="Tomcat 9.0.74 (remm)" rtext="2023-04-18">
   <subsection name="Catalina">


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

Reply via email to