Skip to content

Commit dcaf74c

Browse files
ZOOKEEPER-4843: Encountering an 'Unreasonable Length' error when configuring jute.maxbuffer to 1GB or more
Reviewers: anmolnar, kezhuw Author: arshadmohammad Closes #2175 from arshadmohammad/totalBuffer
1 parent 9df310a commit dcaf74c

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

zookeeper-jute/src/main/java/org/apache/jute/BinaryInputArchive.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ public class BinaryInputArchive implements InputArchive {
4848
}
4949

5050
private final DataInput in;
51-
private final int maxBufferSize;
52-
private final int extraMaxBufferSize;
51+
private final int totalBufferSize;
5352

5453
public static BinaryInputArchive getArchive(InputStream stream) {
5554
return new BinaryInputArchive(new DataInputStream(stream));
@@ -80,8 +79,11 @@ public BinaryInputArchive(DataInput in) {
8079

8180
public BinaryInputArchive(DataInput in, int maxBufferSize, int extraMaxBufferSize) {
8281
this.in = in;
83-
this.maxBufferSize = maxBufferSize;
84-
this.extraMaxBufferSize = extraMaxBufferSize;
82+
if ((long) maxBufferSize + extraMaxBufferSize > Integer.MAX_VALUE) {
83+
this.totalBufferSize = Integer.MAX_VALUE;
84+
} else {
85+
this.totalBufferSize = maxBufferSize + extraMaxBufferSize;
86+
}
8587
}
8688

8789
public byte readByte(String tag) throws IOException {
@@ -162,7 +164,7 @@ public void endMap(String tag) throws IOException {
162164
// make up for extra fields, etc. (otherwise e.g. clients may be able to
163165
// write buffers larger than we can read from disk!)
164166
private void checkLength(int len) throws IOException {
165-
if (len < 0 || len > maxBufferSize + extraMaxBufferSize) {
167+
if (len < 0 || len > totalBufferSize) {
166168
throw new IOException(UNREASONBLE_LENGTH + len);
167169
}
168170
}

zookeeper-jute/src/test/java/org/apache/jute/BinaryInputArchiveTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,4 +195,17 @@ private byte[] getData(int recordSize) {
195195
return buf.array();
196196
}
197197

198+
@Test
199+
public void testTotalBufferSizeShouldNotBeMoreThanIntegerMaxValue()
200+
throws IOException {
201+
int maxBufferSize = 1 * 1024 * 1024 * 1024; // buffer size 1GB
202+
int extraMaxBufferSize = maxBufferSize;
203+
int recordSize = 1000;
204+
BinaryInputArchive ia =
205+
getBinaryInputArchive(recordSize, maxBufferSize, extraMaxBufferSize);
206+
String s = ia.readString("");
207+
assertNotNull(s);
208+
assertEquals(recordSize, s.getBytes().length);
209+
}
210+
198211
}

0 commit comments

Comments
 (0)