From aa214c0b544e6dbd83b51b77900a8e3bf2640a9d Mon Sep 17 00:00:00 2001 From: md_5 Date: Sun, 10 Jan 2016 19:00:00 +1100 Subject: [PATCH] [#1695] Use BoundedArrayList to cap list size for REGISTER channels. --- .../bungee/connection/InitialHandler.java | 4 +- .../md_5/bungee/util/BoundedArrayList.java | 49 +++++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 proxy/src/main/java/net/md_5/bungee/util/BoundedArrayList.java diff --git a/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java b/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java index c13d2e62..8dffca38 100644 --- a/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java +++ b/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java @@ -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.StatusRequest; import net.md_5.bungee.protocol.packet.StatusResponse; +import net.md_5.bungee.util.BoundedArrayList; @RequiredArgsConstructor public class InitialHandler extends PacketHandler implements PendingConnection @@ -73,7 +74,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection private LoginRequest loginRequest; private EncryptionRequest request; @Getter - private final List registerMessages = new ArrayList<>(); + private final List registerMessages = new BoundedArrayList<>(128); private State thisState = State.HANDSHAKE; private final Unsafe unsafe = new Unsafe() { @@ -122,7 +123,6 @@ public class InitialHandler extends PacketHandler implements PendingConnection // TODO: Unregister? if ( pluginMessage.getTag().equals( "REGISTER" ) ) { - Preconditions.checkState( registerMessages.size() < 128, "Too many channels registered" ); registerMessages.add( pluginMessage ); } } diff --git a/proxy/src/main/java/net/md_5/bungee/util/BoundedArrayList.java b/proxy/src/main/java/net/md_5/bungee/util/BoundedArrayList.java new file mode 100644 index 00000000..d4bd1107 --- /dev/null +++ b/proxy/src/main/java/net/md_5/bungee/util/BoundedArrayList.java @@ -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 extends ArrayList +{ + + 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 c) + { + checkSize( c.size() ); + return super.addAll( c ); + } + + @Override + public boolean addAll(int index, Collection c) + { + checkSize( c.size() ); + return super.addAll( index, c ); + } +}