From be41c59b3d676632710f10a1c88d655e12cbf48c Mon Sep 17 00:00:00 2001 From: linsaftw Date: Sat, 20 Mar 2021 12:36:25 -0300 Subject: [PATCH] Fix ByteBuf memory leaks diff --git a/proxy/src/main/java/net/md_5/bungee/ServerConnector.java b/proxy/src/main/java/net/md_5/bungee/ServerConnector.java index 522bd93da..494375cd6 100644 --- a/proxy/src/main/java/net/md_5/bungee/ServerConnector.java +++ b/proxy/src/main/java/net/md_5/bungee/ServerConnector.java @@ -284,9 +284,15 @@ public class ServerConnector extends PacketHandler } else { ByteBuf brand = ByteBufAllocator.DEFAULT.heapBuffer(); - DefinedPacket.writeString(brandString, brand); - user.unsafe().sendPacket( new PluginMessage( user.getPendingConnection().getVersion() >= ProtocolConstants.MINECRAFT_1_13 ? "minecraft:brand" : "MC|Brand", brand, handshakeHandler.isServerForge() ) ); - brand.release(); + + try + { + DefinedPacket.writeString(brandString, brand); + user.unsafe().sendPacket( new PluginMessage( user.getPendingConnection().getVersion() >= ProtocolConstants.MINECRAFT_1_13 ? "minecraft:brand" : "MC|Brand", brand, handshakeHandler.isServerForge() ) ); + } finally + { + brand.release(); + } } // Travertine end } diff --git a/proxy/src/main/java/net/md_5/bungee/connection/DownstreamBridge.java b/proxy/src/main/java/net/md_5/bungee/connection/DownstreamBridge.java index 474551d3e..8716d0c85 100644 --- a/proxy/src/main/java/net/md_5/bungee/connection/DownstreamBridge.java +++ b/proxy/src/main/java/net/md_5/bungee/connection/DownstreamBridge.java @@ -294,12 +294,21 @@ public class DownstreamBridge extends PacketHandler try { ByteBuf brand = Unpooled.wrappedBuffer(pluginMessage.getData()); - String serverBrand = DefinedPacket.readString(brand); - brand.release(); - brand = ByteBufAllocator.DEFAULT.heapBuffer(); - DefinedPacket.writeString(bungee.getName() + " <- " + serverBrand, brand ); // Waterfall - pluginMessage.setData(brand); - brand.release(); + String serverBrand = null; + + try { + serverBrand = DefinedPacket.readString(brand); + } finally { + brand.release(); + } + + try { + brand = ByteBufAllocator.DEFAULT.heapBuffer(); + DefinedPacket.writeString(bungee.getName() + " <- " + serverBrand, brand ); // Waterfall + pluginMessage.setData(brand); + } finally { + brand.release(); + } } catch (Exception ProtocolHacksSuck) { return; diff --git a/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap.java b/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap.java index a3a12e19b..214a1fd04 100644 --- a/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap.java +++ b/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap.java @@ -143,11 +143,17 @@ public abstract class EntityMap if ( readId == oldId || readId == newId ) { ByteBuf data = packet.copy(); - packet.readerIndex( offset ); - packet.writerIndex( offset ); - DefinedPacket.writeVarInt( readId == oldId ? newId : oldId, packet ); - packet.writeBytes( data ); - data.release(); + + try + { + packet.readerIndex( offset ); + packet.writerIndex( offset ); + DefinedPacket.writeVarInt( readId == oldId ? newId : oldId, packet ); + packet.writeBytes( data ); + } finally + { + data.release(); + } } } diff --git a/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap_1_7_6.java b/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap_1_7_6.java index 6755fe845..21871d14a 100644 --- a/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap_1_7_6.java +++ b/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap_1_7_6.java @@ -39,19 +39,27 @@ class EntityMap_1_7_6 extends EntityMap_1_7_2 && profile.getProperties().length >= 1 ) { ByteBuf rest = packet.copy(); - packet.readerIndex( readerIndex ); - packet.writerIndex( readerIndex + packetIdLength + idLength ); - DefinedPacket.writeString( player.getUniqueId().toString(), packet ); - DefinedPacket.writeString( username, packet ); - DefinedPacket.writeVarInt( profile.getProperties().length, packet ); - for ( LoginResult.Property property : profile.getProperties() ) + + try + { + packet.readerIndex( readerIndex ); + packet.writerIndex( readerIndex + packetIdLength + idLength ); + DefinedPacket.writeString( player.getUniqueId().toString(), packet ); + DefinedPacket.writeString( username, packet ); + DefinedPacket.writeVarInt( profile.getProperties().length, packet ); + + for ( LoginResult.Property property : profile.getProperties() ) + { + DefinedPacket.writeString( property.getName(), packet ); + DefinedPacket.writeString( property.getValue(), packet ); + DefinedPacket.writeString( property.getSignature(), packet ); + } + + packet.writeBytes( rest ); + } finally { - DefinedPacket.writeString( property.getName(), packet ); - DefinedPacket.writeString( property.getValue(), packet ); - DefinedPacket.writeString( property.getSignature(), packet ); + rest.release(); } - packet.writeBytes( rest ); - rest.release(); } } } diff --git a/proxy/src/main/java/net/md_5/bungee/forge/ForgeUtils.java b/proxy/src/main/java/net/md_5/bungee/forge/ForgeUtils.java index cefa0206e..2cd5740ca 100644 --- a/proxy/src/main/java/net/md_5/bungee/forge/ForgeUtils.java +++ b/proxy/src/main/java/net/md_5/bungee/forge/ForgeUtils.java @@ -38,15 +38,27 @@ public class ForgeUtils { Map modTags = new HashMap<>(); ByteBuf payload = Unpooled.wrappedBuffer( pluginMessage.getData() ); - byte discriminator = payload.readByte(); - if ( discriminator == 2 ) // ModList + + try { - ByteBuf buffer = payload.slice(); - int modCount = DefinedPacket.readVarInt( buffer, 2 ); - for ( int i = 0; i < modCount; i++ ) + byte discriminator = payload.readByte(); + if ( discriminator == 2 ) // ModList { - modTags.put( DefinedPacket.readString( buffer ), DefinedPacket.readString( buffer ) ); + ByteBuf buffer = payload.slice(); + + try { + int modCount = DefinedPacket.readVarInt( buffer, 2 ); + for ( int i = 0; i < modCount; i++ ) + { + modTags.put( DefinedPacket.readString( buffer ), DefinedPacket.readString( buffer ) ); + } + } finally + { + buffer.release(); + } } + } finally { + payload.release(); } return modTags; } -- 2.31.1