From c68dac4cbec0b294112e8ad9591a5a4273405487 Mon Sep 17 00:00:00 2001 From: Hannes Braun Date: Fri, 6 Oct 2023 16:34:08 +0200 Subject: [PATCH] Fix IndexOutOfBoundsException for unexpected EOF --- src/main/java/rv/comm/rcssserver/ServerComm.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main/java/rv/comm/rcssserver/ServerComm.java b/src/main/java/rv/comm/rcssserver/ServerComm.java index 48ba0f9..4297b01 100644 --- a/src/main/java/rv/comm/rcssserver/ServerComm.java +++ b/src/main/java/rv/comm/rcssserver/ServerComm.java @@ -152,8 +152,14 @@ private String readMessage() throws IOException // read from stream until all bytes in message are read byte[] buf = new byte[length]; int bytesRead = 0; - while (bytesRead < length) - bytesRead += in.read(buf, bytesRead, length - bytesRead); + while (bytesRead < length) { + int tmp = in.read(buf, bytesRead, length - bytesRead); + if (tmp < 0) { + // Unexpected EOF + return null; + } + bytesRead += tmp; + } return new String(buf); }