Fix ByteBuf memory leak in MinecraftVarintFrameDecoder (#1715)

- Reset buffer reader index on exception to prevent memory leaks when packet decoding fails.
This commit is contained in:
mason
2026-01-21 10:56:22 -08:00
committed by GitHub
parent 21671daebe
commit 3022793418

View File

@@ -91,6 +91,7 @@ public class MinecraftVarintFrameDecoder extends ByteToMessageDecoder {
// try to read the length of the packet // try to read the length of the packet
in.markReaderIndex(); in.markReaderIndex();
try {
int length = readRawVarInt21(in); int length = readRawVarInt21(in);
if (packetStart == in.readerIndex()) { if (packetStart == in.readerIndex()) {
return; return;
@@ -115,6 +116,11 @@ public class MinecraftVarintFrameDecoder extends ByteToMessageDecoder {
out.add(in.readRetainedSlice(length)); out.add(in.readRetainedSlice(length));
} }
} }
} catch (Exception e) {
// Reset buffer to consistent state before propagating exception to prevent memory leaks
in.resetReaderIndex();
throw e;
}
} }
private boolean validateServerboundHandshakePacket(ByteBuf in, int length) throws Exception { private boolean validateServerboundHandshakePacket(ByteBuf in, int length) throws Exception {
@@ -122,6 +128,7 @@ public class MinecraftVarintFrameDecoder extends ByteToMessageDecoder {
state.getProtocolRegistry(direction, ProtocolVersion.MINIMUM_VERSION); state.getProtocolRegistry(direction, ProtocolVersion.MINIMUM_VERSION);
final int index = in.readerIndex(); final int index = in.readerIndex();
try {
final int packetId = readRawVarInt21(in); final int packetId = readRawVarInt21(in);
// Index hasn't changed, we've read nothing // Index hasn't changed, we've read nothing
if (index == in.readerIndex()) { if (index == in.readerIndex()) {
@@ -150,6 +157,11 @@ public class MinecraftVarintFrameDecoder extends ByteToMessageDecoder {
in.readerIndex(index); in.readerIndex(index);
return false; return false;
} catch (Exception e) {
// Reset buffer to consistent state before propagating exception to prevent memory leaks
in.readerIndex(index);
throw e;
}
} }
@Override @Override