From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Andrew Steinborn <git@steinborn.me>
Date: Sun, 5 Jul 2020 22:38:18 -0400
Subject: [PATCH] Optimize NetworkManager Exception Handling


diff --git a/src/main/java/net/minecraft/network/ConnectionProtocol.java b/src/main/java/net/minecraft/network/ConnectionProtocol.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/network/ConnectionProtocol.java
+++ b/src/main/java/net/minecraft/network/ConnectionProtocol.java
@@ -0,0 +0,0 @@ public enum ConnectionProtocol {
 
         @Nullable
         public Packet<?> createPacket(int id, FriendlyByteBuf buf) {
+            if (id < 0 || id >= this.idToDeserializer.size()) return null; // Paper
             Function<FriendlyByteBuf, ? extends Packet<T>> function = this.idToDeserializer.get(id);
             return function != null ? function.apply(buf) : null;
         }
diff --git a/src/main/java/net/minecraft/network/Varint21FrameDecoder.java b/src/main/java/net/minecraft/network/Varint21FrameDecoder.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/network/Varint21FrameDecoder.java
+++ b/src/main/java/net/minecraft/network/Varint21FrameDecoder.java
@@ -0,0 +0,0 @@ import io.netty.handler.codec.CorruptedFrameException;
 import java.util.List;
 
 public class Varint21FrameDecoder extends ByteToMessageDecoder {
+    private final byte[] lenBuf = new byte[3]; // Paper
     @Override
     protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) {
+        // Paper start - if channel is not active just discard the packet
+        if (!channelHandlerContext.channel().isActive()) {
+            byteBuf.skipBytes(byteBuf.readableBytes());
+            return;
+        }
+        // Paper end
         byteBuf.markReaderIndex();
+        // Paper start - reuse temporary length buffer
+        byte[] abyte = lenBuf;
+        java.util.Arrays.fill(abyte, (byte) 0);
+        // Paper end
         byte[] bs = new byte[3];
 
         for(int i = 0; i < bs.length; ++i) {
diff --git a/src/main/java/net/minecraft/network/protocol/PacketUtils.java b/src/main/java/net/minecraft/network/protocol/PacketUtils.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/network/protocol/PacketUtils.java
+++ b/src/main/java/net/minecraft/network/protocol/PacketUtils.java
@@ -0,0 +0,0 @@
 package net.minecraft.network.protocol;
 
+import net.minecraft.network.Connection;
 import net.minecraft.network.PacketListener;
+import net.minecraft.network.chat.TextComponent;
+import net.minecraft.network.protocol.game.ClientboundDisconnectPacket;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import co.aikar.timings.MinecraftTimings; // Paper
@@ -0,0 +0,0 @@ public class PacketUtils {
                     try (Timing ignored = timing.startTiming()) { // Paper - timings
                     packet.handle(listener);
                     } // Paper - timings
+                    // Paper start
+                    catch (Exception e) {
+                        Connection networkmanager = listener.getConnection();
+                        if (networkmanager.getPlayer() != null) {
+                            LOGGER.error("Error whilst processing packet {} for {}[{}]", packet, networkmanager.getPlayer().getScoreboardName(), networkmanager.getRemoteAddress(), e);
+                        } else {
+                            LOGGER.error("Error whilst processing packet {} for connection from {}", packet, networkmanager.getRemoteAddress(), e);
+                        }
+                        TextComponent error = new TextComponent("Packet processing error");
+                        networkmanager.send(new ClientboundDisconnectPacket(error), (future) -> {
+                            networkmanager.disconnect(error);
+                        });
+                        networkmanager.setReadOnly();
+                    }
+                    // Paper end
                 } else {
                     PacketUtils.LOGGER.debug("Ignoring packet due to disconnection: {}", packet);
                 }
