From c14ceb315dae5d623dab8679de3234a7f0f67321 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Wed, 1 Jan 2020 10:14:15 -0500 Subject: [PATCH] Return the magic-3 loop again... :( --- .../netty/MinecraftCompressDecoder.java | 1 - .../netty/MinecraftVarintFrameDecoder.java | 32 ++++++++++++++----- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftCompressDecoder.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftCompressDecoder.java index ebf7f927..cf68e134 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftCompressDecoder.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftCompressDecoder.java @@ -30,7 +30,6 @@ public class MinecraftCompressDecoder extends MessageToMessageDecoder { if (claimedUncompressedSize == 0) { // Strip the now-useless uncompressed size, this message is already uncompressed. out.add(in.retainedSlice()); - in.skipBytes(in.readableBytes()); return; } 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 c8fec71a..3adc8ac3 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 @@ -4,22 +4,38 @@ import com.velocitypowered.proxy.protocol.ProtocolUtils; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.ByteToMessageDecoder; +import io.netty.handler.codec.CorruptedFrameException; import java.util.List; public class MinecraftVarintFrameDecoder extends ByteToMessageDecoder { @Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { - while (in.isReadable()) { - int ri = in.readerIndex(); - int packetLength = ProtocolUtils.readVarInt(in); + read_lens: while (in.isReadable()) { + int origReaderIndex = in.readerIndex(); + for (int i = 0; i < 3; i++) { + if (!in.isReadable()) { + in.readerIndex(origReaderIndex); + return; + } - if (in.readableBytes() >= packetLength) { - out.add(in.readBytes(packetLength)); - } else { - in.readerIndex(ri); - break; + byte read = in.readByte(); + if (read >= 0) { + // Make sure reader index of length buffer is returned to the beginning + in.readerIndex(origReaderIndex); + int packetLength = ProtocolUtils.readVarInt(in); + + if (in.readableBytes() >= packetLength) { + out.add(in.readBytes(packetLength)); + continue read_lens; + } else { + in.readerIndex(origReaderIndex); + return; + } + } } + + throw new CorruptedFrameException("VarInt too big"); } } }