Improve reliability of varint decoder.
This commit is contained in:
@@ -2,8 +2,10 @@ 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.List;
|
||||
|
||||
@@ -15,12 +17,26 @@ public class MinecraftVarintFrameDecoder extends ByteToMessageDecoder {
|
||||
}
|
||||
|
||||
in.markReaderIndex();
|
||||
int packetLength = ProtocolUtils.readVarInt(in);
|
||||
if (in.readableBytes() < packetLength) {
|
||||
in.resetReaderIndex();
|
||||
return;
|
||||
|
||||
byte[] lenBuf = new byte[3];
|
||||
for (int i = 0; i < lenBuf.length; i++) {
|
||||
lenBuf[i] = in.readByte();
|
||||
if (lenBuf[i] > 0) {
|
||||
int packetLength = ProtocolUtils.readVarInt(Unpooled.wrappedBuffer(lenBuf));
|
||||
if (packetLength == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (in.readableBytes() < packetLength) {
|
||||
in.resetReaderIndex();
|
||||
return;
|
||||
}
|
||||
|
||||
out.add(in.readRetainedSlice(packetLength));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
out.add(in.readRetainedSlice(packetLength));
|
||||
throw new CorruptedFrameException("VarInt too big");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user