aboutsummaryrefslogtreecommitdiffhomepage
path: root/Spigot-Server-Patches/0550-Optimize-NetworkManager-Exception-Handling.patch
blob: e6b4f649a380cbea9dd9e59b606f5de9387337ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Andrew Steinborn <git@steinborn.me>
Date: Sun, 5 Jul 2020 22:59:38 -0400
Subject: [PATCH] Optimize NetworkManager Exception Handling


diff --git a/src/main/java/net/minecraft/server/EnumProtocol.java b/src/main/java/net/minecraft/server/EnumProtocol.java
index 7cba1edc3f2e8fb7cfa730efa0fb3cc5580f12e1..300286354f58d7761ede06ab2df8d42cfe3d936e 100644
--- a/src/main/java/net/minecraft/server/EnumProtocol.java
+++ b/src/main/java/net/minecraft/server/EnumProtocol.java
@@ -128,6 +128,7 @@ public enum EnumProtocol {
 
         @Nullable
         public Packet<?> a(int i) {
+            if (i < 0 || i >= this.b.size()) return null; // Paper
             Supplier<? extends Packet<T>> supplier = (Supplier) this.b.get(i);
 
             return supplier != null ? (Packet) supplier.get() : null;
diff --git a/src/main/java/net/minecraft/server/PacketSplitter.java b/src/main/java/net/minecraft/server/PacketSplitter.java
index cdaa8be90d16b0e9e2f92a3e2ed120b856feab54..2aaa8770edfd8acc6861c23176e405863858b275 100644
--- a/src/main/java/net/minecraft/server/PacketSplitter.java
+++ b/src/main/java/net/minecraft/server/PacketSplitter.java
@@ -9,11 +9,21 @@ import java.util.List;
 
 public class PacketSplitter extends ByteToMessageDecoder {
 
+    private final byte[] lenBuf = new byte[3]; // Paper
     public PacketSplitter() {}
 
     protected void decode(ChannelHandlerContext channelhandlercontext, ByteBuf bytebuf, List<Object> list) throws Exception {
+        // Paper start - if channel is not active just discard the packet
+        if (!channelhandlercontext.channel().isActive()) {
+            bytebuf.skipBytes(bytebuf.readableBytes());
+            return;
+        }
+        // Paper end
         bytebuf.markReaderIndex();
-        byte[] abyte = new byte[3];
+        // Paper start - reuse temporary length buffer
+        byte[] abyte = lenBuf;
+        java.util.Arrays.fill(abyte, (byte) 0);
+        // Paper end
 
         for (int i = 0; i < abyte.length; ++i) {
             if (!bytebuf.isReadable()) {
diff --git a/src/main/java/net/minecraft/server/PlayerConnectionUtils.java b/src/main/java/net/minecraft/server/PlayerConnectionUtils.java
index eb3269e0ea3ce33d08e9eee3bca7cf434921e991..7ea293f38dedd6066601d94adbe175a31c502e1f 100644
--- a/src/main/java/net/minecraft/server/PlayerConnectionUtils.java
+++ b/src/main/java/net/minecraft/server/PlayerConnectionUtils.java
@@ -22,6 +22,21 @@ public class PlayerConnectionUtils {
                     try (Timing ignored = timing.startTiming()) { // Paper - timings
                     packet.a(t0);
                     } // Paper - timings
+                    // Paper start
+                    catch (Exception e) {
+                        NetworkManager networkmanager = t0.a();
+                        if (networkmanager.getPlayer() != null) {
+                            LOGGER.error("Error whilst processing packet {} for {}[{}]", packet, networkmanager.getPlayer().getName(), networkmanager.getSocketAddress(), e);
+                        } else {
+                            LOGGER.error("Error whilst processing packet {} for connection from {}", packet, networkmanager.getSocketAddress(), e);
+                        }
+                        ChatComponentText error = new ChatComponentText("Packet processing error");
+                        networkmanager.sendPacket(new PacketPlayOutKickDisconnect(error), (future) -> {
+                            networkmanager.close(error);
+                        });
+                        networkmanager.stopReading();
+                    }
+                    // Paper end
                 } else {
                     PlayerConnectionUtils.LOGGER.debug("Ignoring packet due to disconnection: " + packet);
                 }