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

andor pushed a commit to branch branch-3.8
in repository https://gitbox.apache.org/repos/asf/zookeeper.git


The following commit(s) were added to refs/heads/branch-3.8 by this push:
     new 75662182a ZOOKEEPER-4843: Encountering an 'Unreasonable Length' error 
when configuring jute.maxbuffer to 1GB or more
75662182a is described below

commit 75662182a9d1b595762564cc22c6dd7338e2c41c
Author: Mohammad Arshad <[email protected]>
AuthorDate: Fri Sep 27 02:54:22 2024 +0530

    ZOOKEEPER-4843: Encountering an 'Unreasonable Length' error when 
configuring jute.maxbuffer to 1GB or more
    
    Reviewers: kezhuw, anmolnar
    Author: arshadmohammad
    Closes #2191 from arshadmohammad/totalBuffer-branch-3.8
---
 .../src/main/java/org/apache/jute/BinaryInputArchive.java  | 14 ++++++++------
 .../test/java/org/apache/jute/BinaryInputArchiveTest.java  | 13 +++++++++++++
 2 files changed, 21 insertions(+), 6 deletions(-)

diff --git 
a/zookeeper-jute/src/main/java/org/apache/jute/BinaryInputArchive.java 
b/zookeeper-jute/src/main/java/org/apache/jute/BinaryInputArchive.java
index c4d1bbebf..20823f29b 100644
--- a/zookeeper-jute/src/main/java/org/apache/jute/BinaryInputArchive.java
+++ b/zookeeper-jute/src/main/java/org/apache/jute/BinaryInputArchive.java
@@ -47,9 +47,8 @@ public class BinaryInputArchive implements InputArchive {
         }
     }
 
-    private DataInput in;
-    private int maxBufferSize;
-    private int extraMaxBufferSize;
+    private final DataInput in;
+    private final int totalBufferSize;
 
     public static BinaryInputArchive getArchive(InputStream strm) {
         return new BinaryInputArchive(new DataInputStream(strm));
@@ -80,8 +79,11 @@ public class BinaryInputArchive implements InputArchive {
 
     public BinaryInputArchive(DataInput in, int maxBufferSize, int 
extraMaxBufferSize) {
         this.in = in;
-        this.maxBufferSize = maxBufferSize;
-        this.extraMaxBufferSize = extraMaxBufferSize;
+        if ((long) maxBufferSize + extraMaxBufferSize > Integer.MAX_VALUE) {
+            this.totalBufferSize = Integer.MAX_VALUE;
+        } else {
+            this.totalBufferSize = maxBufferSize + extraMaxBufferSize;
+        }
     }
 
     public byte readByte(String tag) throws IOException {
@@ -162,7 +164,7 @@ public class BinaryInputArchive implements InputArchive {
     // make up for extra fields, etc. (otherwise e.g. clients may be able to
     // write buffers larger than we can read from disk!)
     private void checkLength(int len) throws IOException {
-        if (len < 0 || len > maxBufferSize + extraMaxBufferSize) {
+        if (len < 0 || len > totalBufferSize) {
             throw new IOException(UNREASONBLE_LENGTH + len);
         }
     }
diff --git 
a/zookeeper-jute/src/test/java/org/apache/jute/BinaryInputArchiveTest.java 
b/zookeeper-jute/src/test/java/org/apache/jute/BinaryInputArchiveTest.java
index 9a1c70eeb..294a8b129 100644
--- a/zookeeper-jute/src/test/java/org/apache/jute/BinaryInputArchiveTest.java
+++ b/zookeeper-jute/src/test/java/org/apache/jute/BinaryInputArchiveTest.java
@@ -195,4 +195,17 @@ public class BinaryInputArchiveTest {
     return buf.array();
   }
 
+    @Test
+    public void testTotalBufferSizeShouldNotBeMoreThanIntegerMaxValue()
+            throws IOException {
+        int maxBufferSize = 1 * 1024 * 1024 * 1024; // buffer size 1GB
+        int extraMaxBufferSize = maxBufferSize;
+        int recordSize = 1000;
+        BinaryInputArchive ia =
+                getBinaryInputArchive(recordSize, maxBufferSize, 
extraMaxBufferSize);
+        String s = ia.readString("");
+        assertNotNull(s);
+        assertEquals(recordSize, s.getBytes().length);
+    }
+
 }

Reply via email to