mirror of
https://github.com/PaperMC/Velocity.git
synced 2026-02-17 06:27:42 +01:00
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:
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user