From c2e8927c4209822d57ffc95e0d91e7b3089e9674 Mon Sep 17 00:00:00 2001 From: pixtaded Date: Sun, 12 Jan 2025 15:06:43 +0300 Subject: [PATCH] Fix server throwing an ArrayOutOfBoundsException when someone tries to leave --- src/main/java/net/pixtaded/crab/server/ServerThread.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/pixtaded/crab/server/ServerThread.java b/src/main/java/net/pixtaded/crab/server/ServerThread.java index 03006e4..2c13324 100644 --- a/src/main/java/net/pixtaded/crab/server/ServerThread.java +++ b/src/main/java/net/pixtaded/crab/server/ServerThread.java @@ -30,10 +30,13 @@ public class ServerThread implements Runnable { public void run() { try { byte[] PID = input.readNBytes(1); + if (PID.length == 0) { + socket.close(); + return; + } switch (PID[0]) { case MESSAGE -> { server.getDb().logMessage(new Date(), socket.getInetAddress().getHostAddress(), new String(input.readNBytes(4096), StandardCharsets.UTF_8).trim()); - socket.close(); } case LOGS -> { respond(server.getDb().getLogs()); } case LOGS_SIZE -> { @@ -42,6 +45,7 @@ public class ServerThread implements Runnable { System.out.println("PID not implemented: " + PID[0]); } } + socket.close(); } catch (Exception e) { throw new RuntimeException(e); } @@ -50,7 +54,6 @@ public class ServerThread implements Runnable { private void respond(byte[] data) throws IOException { socket.getOutputStream().write(data); socket.getOutputStream().flush(); - socket.close(); } private void respond(String utf8) throws IOException {