[#1695] Use BoundedArrayList to cap list size for REGISTER channels.

This commit is contained in:
md_5 2016-01-10 19:00:00 +11:00
parent 81bd3b5f71
commit aa214c0b54
2 changed files with 51 additions and 2 deletions

View File

@ -58,6 +58,7 @@ import net.md_5.bungee.protocol.packet.LoginSuccess;
import net.md_5.bungee.protocol.packet.PingPacket; import net.md_5.bungee.protocol.packet.PingPacket;
import net.md_5.bungee.protocol.packet.StatusRequest; import net.md_5.bungee.protocol.packet.StatusRequest;
import net.md_5.bungee.protocol.packet.StatusResponse; import net.md_5.bungee.protocol.packet.StatusResponse;
import net.md_5.bungee.util.BoundedArrayList;
@RequiredArgsConstructor @RequiredArgsConstructor
public class InitialHandler extends PacketHandler implements PendingConnection public class InitialHandler extends PacketHandler implements PendingConnection
@ -73,7 +74,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection
private LoginRequest loginRequest; private LoginRequest loginRequest;
private EncryptionRequest request; private EncryptionRequest request;
@Getter @Getter
private final List<PluginMessage> registerMessages = new ArrayList<>(); private final List<PluginMessage> registerMessages = new BoundedArrayList<>(128);
private State thisState = State.HANDSHAKE; private State thisState = State.HANDSHAKE;
private final Unsafe unsafe = new Unsafe() private final Unsafe unsafe = new Unsafe()
{ {
@ -122,7 +123,6 @@ public class InitialHandler extends PacketHandler implements PendingConnection
// TODO: Unregister? // TODO: Unregister?
if ( pluginMessage.getTag().equals( "REGISTER" ) ) if ( pluginMessage.getTag().equals( "REGISTER" ) )
{ {
Preconditions.checkState( registerMessages.size() < 128, "Too many channels registered" );
registerMessages.add( pluginMessage ); registerMessages.add( pluginMessage );
} }
} }

View File

@ -0,0 +1,49 @@
package net.md_5.bungee.util;
import com.google.common.base.Preconditions;
import java.util.ArrayList;
import java.util.Collection;
public class BoundedArrayList<E> extends ArrayList<E>
{
private final int maxSize;
public BoundedArrayList(int maxSize)
{
this.maxSize = maxSize;
}
private void checkSize(int increment)
{
Preconditions.checkState( size() + increment < maxSize, "Adding %s elements would exceed capacity of %s", increment, maxSize );
}
@Override
public boolean add(E e)
{
checkSize( 1 );
return super.add( e );
}
@Override
public void add(int index, E element)
{
checkSize( 1 );
super.add( index, element );
}
@Override
public boolean addAll(Collection<? extends E> c)
{
checkSize( c.size() );
return super.addAll( c );
}
@Override
public boolean addAll(int index, Collection<? extends E> c)
{
checkSize( c.size() );
return super.addAll( index, c );
}
}