You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ratis.apache.org by sz...@apache.org on 2021/03/07 12:38:09 UTC

[ratis] branch master updated: RATIS-1330. fix ratis-examples SubCommand parsePeers method ArrayIndexOutOfBounds exception (#431)

This is an automated email from the ASF dual-hosted git repository.

szetszwo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ratis.git


The following commit(s) were added to refs/heads/master by this push:
     new 99f7933  RATIS-1330. fix ratis-examples SubCommand parsePeers method ArrayIndexOutOfBounds exception (#431)
99f7933 is described below

commit 99f7933f6be25f0598dc43ca9066efe923e266ac
Author: Dawei Liu <li...@apache.org>
AuthorDate: Sun Mar 7 20:38:02 2021 +0800

    RATIS-1330. fix ratis-examples SubCommand parsePeers method ArrayIndexOutOfBounds exception (#431)
---
 .../ratis/examples/common/SubCommandBase.java      |  5 +++
 .../ratis/examples/common/TestSubCommand.java      | 44 ++++++++++++++++++++++
 2 files changed, 49 insertions(+)

diff --git a/ratis-examples/src/main/java/org/apache/ratis/examples/common/SubCommandBase.java b/ratis-examples/src/main/java/org/apache/ratis/examples/common/SubCommandBase.java
index d879691..d650a5c 100644
--- a/ratis-examples/src/main/java/org/apache/ratis/examples/common/SubCommandBase.java
+++ b/ratis-examples/src/main/java/org/apache/ratis/examples/common/SubCommandBase.java
@@ -43,6 +43,11 @@ public abstract class SubCommandBase {
   public static RaftPeer[] parsePeers(String peers) {
     return Stream.of(peers.split(",")).map(address -> {
       String[] addressParts = address.split(":");
+      if (addressParts.length < 3) {
+        throw new IllegalArgumentException(
+            "Raft peer " + address + " is not a legitimate format. "
+                + "(format: name:host:port:dataStreamPort:clientPort:adminPort)");
+      }
       RaftPeer.Builder builder = RaftPeer.newBuilder();
       builder.setId(addressParts[0]).setAddress(addressParts[1] + ":" + addressParts[2]);
       if (addressParts.length >= 4) {
diff --git a/ratis-examples/src/test/java/org/apache/ratis/examples/common/TestSubCommand.java b/ratis-examples/src/test/java/org/apache/ratis/examples/common/TestSubCommand.java
new file mode 100644
index 0000000..71fe7e6
--- /dev/null
+++ b/ratis-examples/src/test/java/org/apache/ratis/examples/common/TestSubCommand.java
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.ratis.examples.common;
+
+import java.util.Collection;
+import java.util.Collections;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+@RunWith(Parameterized.class)
+public class TestSubCommand {
+
+  @Parameterized.Parameters
+  public static Collection<String> data() {
+    return Collections.singleton("127.0.0.1:6667");
+  }
+
+  @Parameterized.Parameter
+  public String peers;
+
+  @Test(expected = IllegalArgumentException.class)
+  public void testParsePeers() {
+    SubCommandBase.parsePeers(peers);
+  }
+
+}