From c885ca1d7c027919bb62a716a91e5687f998de67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noel=20N=C3=A9meth?= Date: Sun, 17 Jul 2022 20:28:14 +0200 Subject: [PATCH] Fix index out of bounds when creating a command packet (#1243) --- .../minestom/server/command/GraphConverter.java | 16 ++++++++-------- .../server/command/CommandPacketTest.java | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/main/java/net/minestom/server/command/GraphConverter.java b/src/main/java/net/minestom/server/command/GraphConverter.java index f01dcee34..a9424257a 100644 --- a/src/main/java/net/minestom/server/command/GraphConverter.java +++ b/src/main/java/net/minestom/server/command/GraphConverter.java @@ -42,14 +42,14 @@ final class GraphConverter { final DeclareCommandsPacket.Node node = new DeclareCommandsPacket.Node(); int[] packetNodeChildren = new int[children.size()]; - for (int i = 0; i < packetNodeChildren.length; i++) { + for (int i = 0, appendIndex = 0; i < children.size(); i++) { final int[] append = append(children.get(i), to, rootRedirect, id, redirect, redirectSetters, player); if (append.length == 1) { - packetNodeChildren[i] = append[0]; + packetNodeChildren[appendIndex++] = append[0]; } else { packetNodeChildren = Arrays.copyOf(packetNodeChildren, packetNodeChildren.length + append.length - 1); - System.arraycopy(append, 0, packetNodeChildren, i, append.length); - i += append.length; + System.arraycopy(append, 0, packetNodeChildren, appendIndex, append.length); + appendIndex += append.length; } } node.children = packetNodeChildren; @@ -125,15 +125,15 @@ final class GraphConverter { List setters = new ArrayList<>(); int[] res = new int[special.arguments().size()]; List arguments = special.arguments(); - for (int i = 0; i < arguments.size(); i++) { + for (int i = 0, appendIndex = 0; i < arguments.size(); i++) { Object arg = arguments.get(i); final int[] append = append(new GraphImpl.NodeImpl((Argument) arg, null, List.of()), to, rootRedirect, id, r, setters, player); if (append.length == 1) { - res[i] = append[0]; + res[appendIndex++] = append[0]; } else { res = Arrays.copyOf(res, res.length + append.length - 1); - System.arraycopy(append, 0, res, i, append.length); - i += append.length; + System.arraycopy(append, 0, res, appendIndex, append.length); + appendIndex += append.length; } } r.set(id.get()); diff --git a/src/test/java/net/minestom/server/command/CommandPacketTest.java b/src/test/java/net/minestom/server/command/CommandPacketTest.java index f064de7e5..18fdb74ba 100644 --- a/src/test/java/net/minestom/server/command/CommandPacketTest.java +++ b/src/test/java/net/minestom/server/command/CommandPacketTest.java @@ -154,6 +154,20 @@ public class CommandPacketTest { int3->int4 """, graph); } + @Test + public void twoEnumAndOneLiteralChild() { + var graph = Graph.builder(ArgumentType.Literal("foo")) + .append(ArgumentType.Enum("a", A.class)) + .append(ArgumentType.Literal("l")) + .append(ArgumentType.Enum("b", B.class)) + .build(); + assertPacketGraph(""" + foo l=% + 0->foo + a b c d e f=ยง + foo->a b c d e f l + """, graph); + } static void assertPacketGraph(String expected, Graph... graphs) { var packet = GraphConverter.createPacket(Graph.merge(graphs), null);