Waterfall/Waterfall-Proxy-Patches/0026-Fix-ByteBuf-memory-leaks.patch
2021-05-19 22:38:23 -03:00

168 lines
8.0 KiB
Diff

From 70d54b4ecbdca409826513d00c0a4d53d6603a11 Mon Sep 17 00:00:00 2001
From: linsaftw <linsaftw@users.noreply.github.com>
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 522bd93d..494375cd 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 474551d3..8716d0c8 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 44066317..e4b97490 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
@@ -145,11 +145,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 6755fe84..21871d14 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 cefa0206..cede3994 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,22 @@ public class ForgeUtils
{
Map<String, String> 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();
+
+ int modCount = DefinedPacket.readVarInt( buffer, 2 );
+ for ( int i = 0; i < modCount; i++ )
+ {
+ modTags.put( DefinedPacket.readString( buffer ), DefinedPacket.readString( buffer ) );
+ }
}
+ } finally {
+ payload.release();
}
return modTags;
}
--
2.31.1