From 7d6fa11a83e438c52979fb9ca5d89d19cb44279a Mon Sep 17 00:00:00 2001 From: creeper123123321 Date: Sun, 13 Jan 2019 08:57:54 -0200 Subject: [PATCH] Don't use ByteBuf, fix varint with 0 ending --- .../netty/MinecraftVarintFrameDecoder.java | 48 ++++++++----------- 1 file changed, 20 insertions(+), 28 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 10ffcb40b..6a114075c 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 @@ -2,11 +2,9 @@ package com.velocitypowered.proxy.protocol.netty; import com.velocitypowered.proxy.protocol.ProtocolUtils; import io.netty.buffer.ByteBuf; -import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.ByteToMessageDecoder; import io.netty.handler.codec.CorruptedFrameException; -import java.util.Arrays; import java.util.List; public class MinecraftVarintFrameDecoder extends ByteToMessageDecoder { @@ -17,38 +15,32 @@ public class MinecraftVarintFrameDecoder extends ByteToMessageDecoder { return; } - ByteBuf lenBuf = ctx.alloc().buffer(3).writeZero(3); int origReaderIndex = in.readerIndex(); - try { - for (int i = 0; i < 3; i++) { - if (!in.isReadable()) { + for (int i = 0; i < 3; i++) { + if (!in.isReadable()) { + in.readerIndex(origReaderIndex); + return; + } + + 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 (packetLength == 0) { + return; + } + + if (in.readableBytes() < packetLength) { in.readerIndex(origReaderIndex); return; } - byte read = in.readByte(); - lenBuf.setByte(i, read); - if (read > 0) { - // Make sure reader index of length buffer is returned to the beginning - lenBuf.readerIndex(0); - int packetLength = ProtocolUtils.readVarInt(lenBuf); - if (packetLength == 0) { - return; - } - - if (in.readableBytes() < packetLength) { - in.readerIndex(origReaderIndex); - return; - } - - out.add(in.readRetainedSlice(packetLength)); - return; - } + out.add(in.readRetainedSlice(packetLength)); + return; } - - throw new CorruptedFrameException("VarInt too big"); - } finally { - lenBuf.release(); } + + throw new CorruptedFrameException("VarInt too big"); } }