diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/ClientCnxnSocket.java b/zookeeper-server/src/main/java/org/apache/zookeeper/ClientCnxnSocket.java index 315d81feb8b..9b53107e3d7 100644 --- a/zookeeper-server/src/main/java/org/apache/zookeeper/ClientCnxnSocket.java +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/ClientCnxnSocket.java @@ -117,7 +117,7 @@ void updateLastSendAndHeard() { void readLength() throws IOException { int len = incomingBuffer.getInt(); - if (len < 0 || len >= packetLen) { + if (len < 0 || len > packetLen) { throw new IOException("Packet len " + len + " is out of range!"); } incomingBuffer = ByteBuffer.allocate(len); diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/ClientCnxnSocketTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/ClientCnxnSocketTest.java index a24b13fdffb..e707cb332d8 100644 --- a/zookeeper-server/src/test/java/org/apache/zookeeper/ClientCnxnSocketTest.java +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/ClientCnxnSocketTest.java @@ -18,6 +18,7 @@ package org.apache.zookeeper; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; import java.io.IOException; @@ -63,4 +64,30 @@ public void testWhenInvalidJuteMaxBufferIsConfiguredIOExceptionIsThrown() { } + /* + * Tests readLength(): + * 1. successfully read packet if length == jute.maxbuffer; + * 2. IOException is thrown if packet length is greater than jute.maxbuffer. + */ + @Test + public void testIOExceptionIsThrownWhenPacketLenExceedsJuteMaxBuffer() throws IOException { + ClientCnxnSocket clientCnxnSocket = new ClientCnxnSocketNIO(new ZKClientConfig()); + + // Should successfully read packet length == jute.maxbuffer + int length = ZKClientConfig.CLIENT_MAX_PACKET_LENGTH_DEFAULT; + clientCnxnSocket.incomingBuffer.putInt(length); + clientCnxnSocket.incomingBuffer.rewind(); + clientCnxnSocket.readLength(); + + // Failed to read packet length > jute.maxbuffer + length = ZKClientConfig.CLIENT_MAX_PACKET_LENGTH_DEFAULT + 1; + clientCnxnSocket.incomingBuffer.putInt(length); + clientCnxnSocket.incomingBuffer.rewind(); + try { + clientCnxnSocket.readLength(); + fail("IOException is expected."); + } catch (IOException e) { + assertEquals("Packet len " + length + " is out of range!", e.getMessage()); + } + } }