Просмотр исходного кода

ZOOKEEPER-2467: NullPointerException when redo Command is passed negative value (Rakesh Kumar Singh via rakeshr)

Rakesh Radhakrishnan 8 лет назад
Родитель
Сommit
422058c222

+ 3 - 0
CHANGES.txt

@@ -397,6 +397,9 @@ BUGFIXES:
   ZOOKEEPER-2611: zoo_remove_watchers - can remove the wrong watch
   (Eyal Leshmem via rgs)
 
+  ZOOKEEPER-2467: NullPointerException when redo Command is passed negative value
+ (Rakesh Kumar Singh via rakeshr)
+
 IMPROVEMENTS:
   ZOOKEEPER-2024 Major throughput improvement with mixed workloads (Kfir Lev-Ari via shralex)
 

+ 1 - 1
src/java/main/org/apache/zookeeper/ZooKeeperMain.java

@@ -616,7 +616,7 @@ public class ZooKeeperMain {
             System.exit(exitCode);
         } else if (cmd.equals("redo") && args.length >= 2) {
             Integer i = Integer.decode(args[1]);
-            if (commandCount <= i) { // don't allow redoing this redo
+            if (commandCount <= i || i < 0) { // don't allow redoing this redo
                 throw new MalformedCommandException("Command index out of range");
             }
             cl.parseCommand(history.get(i));

+ 22 - 0
src/java/test/org/apache/zookeeper/ZooKeeperTest.java

@@ -433,6 +433,28 @@ public class ZooKeeperTest extends ClientBase {
         }
     }
 
+    // ZOOKEEPER-2467 : Testing negative number for redo command
+    @Test
+    public void testRedoWithNegativeCmdNumber() throws Exception {
+        final ZooKeeper zk = createClient();
+        ZooKeeperMain zkMain = new ZooKeeperMain(zk);
+        String cmd1 = "redo -1";
+
+        // setup redirect out/err streams to get System.in/err, use this
+        // judiciously!
+        final PrintStream systemErr = System.err; // get current err
+        final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
+        System.setErr(new PrintStream(errContent));
+        try {
+            zkMain.executeLine(cmd1);
+            Assert.assertEquals("Command index out of range", errContent
+                    .toString().trim());
+        } finally {
+            // revert redirect of out/err streams - important step!
+            System.setErr(systemErr);
+        }
+    }
+
     private static void runCommandExpect(CliCommand command, List<String> expectedResults)
             throws Exception {
         // call command and put result in byteStream