Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}
}