Fix index out of bounds when creating a command packet (#1243)

This commit is contained in:
Noel Németh 2022-07-17 20:28:14 +02:00 committed by GitHub
parent 284da06578
commit c885ca1d7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 8 deletions

View File

@ -42,14 +42,14 @@ final class GraphConverter {
final DeclareCommandsPacket.Node node = new DeclareCommandsPacket.Node(); final DeclareCommandsPacket.Node node = new DeclareCommandsPacket.Node();
int[] packetNodeChildren = new int[children.size()]; 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); final int[] append = append(children.get(i), to, rootRedirect, id, redirect, redirectSetters, player);
if (append.length == 1) { if (append.length == 1) {
packetNodeChildren[i] = append[0]; packetNodeChildren[appendIndex++] = append[0];
} else { } else {
packetNodeChildren = Arrays.copyOf(packetNodeChildren, packetNodeChildren.length + append.length - 1); packetNodeChildren = Arrays.copyOf(packetNodeChildren, packetNodeChildren.length + append.length - 1);
System.arraycopy(append, 0, packetNodeChildren, i, append.length); System.arraycopy(append, 0, packetNodeChildren, appendIndex, append.length);
i += append.length; appendIndex += append.length;
} }
} }
node.children = packetNodeChildren; node.children = packetNodeChildren;
@ -125,15 +125,15 @@ final class GraphConverter {
List<Runnable> setters = new ArrayList<>(); List<Runnable> setters = new ArrayList<>();
int[] res = new int[special.arguments().size()]; int[] res = new int[special.arguments().size()];
List<?> arguments = special.arguments(); 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); Object arg = arguments.get(i);
final int[] append = append(new GraphImpl.NodeImpl((Argument<?>) arg, null, List.of()), to, rootRedirect, id, r, setters, player); final int[] append = append(new GraphImpl.NodeImpl((Argument<?>) arg, null, List.of()), to, rootRedirect, id, r, setters, player);
if (append.length == 1) { if (append.length == 1) {
res[i] = append[0]; res[appendIndex++] = append[0];
} else { } else {
res = Arrays.copyOf(res, res.length + append.length - 1); res = Arrays.copyOf(res, res.length + append.length - 1);
System.arraycopy(append, 0, res, i, append.length); System.arraycopy(append, 0, res, appendIndex, append.length);
i += append.length; appendIndex += append.length;
} }
} }
r.set(id.get()); r.set(id.get());

View File

@ -154,6 +154,20 @@ public class CommandPacketTest {
int3->int4 int3->int4
""", graph); """, 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) { static void assertPacketGraph(String expected, Graph... graphs) {
var packet = GraphConverter.createPacket(Graph.merge(graphs), null); var packet = GraphConverter.createPacket(Graph.merge(graphs), null);