Skip to content

Commit 507da1a

Browse files
authored
ZOOKEEPER-4281: Allow packet of max packet length to be deserialized (#48)
ZOOKEEPER-4281: Allow packet of max packet length to be deserialized ### Problem Resolves https://issues.apache.org/jira/browse/ZOOKEEPER-4281 There are sanity checks for packet size when deserializing a packet. One place has the inclusion: len >= packetLen. It rejects to deserialize the bytes that are exactly sized jute.maxbuffer. It's confusing. This check should use ">" so the maxbuffer length packet can still be deserialized, like the other checks. ``` if (len < 0 || len >= packetLen) { throw new IOException("Packet len " + len + " is out of range!"); } ``` ### Solution Change: `len >= packetLen` -> `len > packetLen` ``` if (len < 0 || len > packetLen) { ``` Author: Huizhi Lu <[email protected]> Reviewers: Enrico Olivelli <[email protected]>, Mohammad Arshad <[email protected]>, Michael Han <[email protected]> Closes apache#1683 from pkuwm/maxPacketLength
1 parent b9ee1db commit 507da1a

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

zookeeper-server/src/main/java/org/apache/zookeeper/ClientCnxnSocket.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ void updateLastSendAndHeard() {
117117

118118
void readLength() throws IOException {
119119
int len = incomingBuffer.getInt();
120-
if (len < 0 || len >= packetLen) {
120+
if (len < 0 || len > packetLen) {
121121
throw new IOException("Packet len " + len + " is out of range!");
122122
}
123123
incomingBuffer = ByteBuffer.allocate(len);

zookeeper-server/src/test/java/org/apache/zookeeper/ClientCnxnSocketTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
package org.apache.zookeeper;
2020

21+
import static org.junit.Assert.assertEquals;
2122
import static org.junit.Assert.assertTrue;
2223
import static org.junit.Assert.fail;
2324
import java.io.IOException;
@@ -63,4 +64,30 @@ public void testWhenInvalidJuteMaxBufferIsConfiguredIOExceptionIsThrown() {
6364

6465
}
6566

67+
/*
68+
* Tests readLength():
69+
* 1. successfully read packet if length == jute.maxbuffer;
70+
* 2. IOException is thrown if packet length is greater than jute.maxbuffer.
71+
*/
72+
@Test
73+
public void testIOExceptionIsThrownWhenPacketLenExceedsJuteMaxBuffer() throws IOException {
74+
ClientCnxnSocket clientCnxnSocket = new ClientCnxnSocketNIO(new ZKClientConfig());
75+
76+
// Should successfully read packet length == jute.maxbuffer
77+
int length = ZKClientConfig.CLIENT_MAX_PACKET_LENGTH_DEFAULT;
78+
clientCnxnSocket.incomingBuffer.putInt(length);
79+
clientCnxnSocket.incomingBuffer.rewind();
80+
clientCnxnSocket.readLength();
81+
82+
// Failed to read packet length > jute.maxbuffer
83+
length = ZKClientConfig.CLIENT_MAX_PACKET_LENGTH_DEFAULT + 1;
84+
clientCnxnSocket.incomingBuffer.putInt(length);
85+
clientCnxnSocket.incomingBuffer.rewind();
86+
try {
87+
clientCnxnSocket.readLength();
88+
fail("IOException is expected.");
89+
} catch (IOException e) {
90+
assertEquals("Packet len " + length + " is out of range!", e.getMessage());
91+
}
92+
}
6693
}

0 commit comments

Comments
 (0)