From dd2e1ad241d2b1a622c6937c1fad9c184cf2320a Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Fri, 29 May 2020 21:11:21 -0400 Subject: [PATCH] Handle partially sent varints better --- .../netty/MinecraftVarintFrameDecoder.java | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftVarintFrameDecoder.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftVarintFrameDecoder.java index 52779d797..68967917f 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftVarintFrameDecoder.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftVarintFrameDecoder.java @@ -20,18 +20,26 @@ public class MinecraftVarintFrameDecoder extends ByteToMessageDecoder { return; } - if (!reader.successfulDecode) { - throw BAD_LENGTH_CACHED; - } + if (reader.successfulDecode) { + if (reader.readVarint < 0) { + throw BAD_LENGTH_CACHED; + } - int minimumRead = reader.bytesRead + reader.readVarint; - if (in.isReadable(minimumRead)) { - out.add(in.retainedSlice(varintEnd + 1, reader.readVarint)); - in.skipBytes(minimumRead); - reader.reset(); + int minimumRead = reader.bytesRead + reader.readVarint; + if (in.isReadable(minimumRead)) { + out.add(in.retainedSlice(varintEnd + 1, reader.readVarint)); + in.skipBytes(minimumRead); + reader.reset(); + } else { + reader.reset(); + return; + } } else { + boolean tooBig = reader.bytesRead > 3; reader.reset(); - return; + if (tooBig) { + throw BAD_LENGTH_CACHED; + } } } }