commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From roxspr...@apache.org
Subject svn commit: r190821 - in /jakarta/commons/proper/cli/trunk/src: java/org/apache/commons/cli2/commandline/Parser.java java/org/apache/commons/cli2/option/GroupImpl.java test/org/apache/commons/cli2/bug/BugLoopingOptionLookAlikeTest.java
Date Wed, 15 Jun 2005 22:13:58 GMT
Author: roxspring
Date: Wed Jun 15 15:13:56 2005
New Revision: 190821

URL: http://svn.apache.org/viewcvs?rev=190821&view=rev
Log:
Fixed second bug where infinite loops could occur.  This 
time it was over eager processing of anonymous arguments in 
a group.  Test case included.

Reported by Steve Alberty

Modified:
    jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/commandline/Parser.java
    jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/GroupImpl.java
    jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/bug/BugLoopingOptionLookAlikeTest.java

Modified: jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/commandline/Parser.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/commandline/Parser.java?rev=190821&r1=190820&r2=190821&view=diff
==============================================================================
--- jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/commandline/Parser.java
(original)
+++ jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/commandline/Parser.java
Wed Jun 15 15:13:56 2005
@@ -17,7 +17,6 @@
 
 import java.io.IOException;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.ListIterator;
@@ -56,7 +55,18 @@
     public CommandLine parse(final String[] arguments) throws OptionException {
         
         // build a mutable list for the arguments
-        final List argumentList = new LinkedList(Arrays.asList(arguments));
+        final List argumentList = new LinkedList();
+        
+        // copy the arguments into the new list
+        for (int i = 0; i < arguments.length; i++) {
+            final String argument = arguments[i];
+            
+            // ensure non intern'd strings are used 
+            // so that == comparisons work as expected
+            argumentList.add(new String(argument));
+        }
+        
+        // wet up a command line for this group
         final WriteableCommandLine commandLine =
             new WriteableCommandLineImpl(group, new ArrayList());
         
@@ -65,7 +75,21 @@
         
         // process the options as far as possible
         final ListIterator iterator = argumentList.listIterator();
+        Object previous = null;
         while (group.canProcess(commandLine, iterator)) {
+            
+            // peek at the next item and backtrack
+            final Object next = iterator.next();
+            iterator.previous();
+            
+            // if we have just tried to process this instance
+            if(next==previous) {
+                // abort
+                break;
+            }
+            // remember previous
+            previous = next;
+            
             group.process(commandLine, iterator);
         }
         

Modified: jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/GroupImpl.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/GroupImpl.java?rev=190821&r1=190820&r2=190821&view=diff
==============================================================================
--- jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/GroupImpl.java
(original)
+++ jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/GroupImpl.java
Wed Jun 15 15:13:56 2005
@@ -160,11 +160,24 @@
         final WriteableCommandLine commandLine,
         final ListIterator arguments)
         throws OptionException {
+        
+        String previous = null;
 
         // [START process each command line token
         while (arguments.hasNext()) {
-       
+            
+            // grab the next argument
             final String arg = (String)arguments.next();
+            
+            // if we have just tried to process this instance
+            if(arg==previous) {
+                // rollback and abort
+                arguments.previous();
+                break;
+            }
+            // remember last processed instance
+            previous = arg;
+            
             final Option opt = (Option)optionMap.get(arg);
 
             // option found

Modified: jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/bug/BugLoopingOptionLookAlikeTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/bug/BugLoopingOptionLookAlikeTest.java?rev=190821&r1=190820&r2=190821&view=diff
==============================================================================
--- jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/bug/BugLoopingOptionLookAlikeTest.java
(original)
+++ jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/bug/BugLoopingOptionLookAlikeTest.java
Wed Jun 15 15:13:56 2005
@@ -17,12 +17,14 @@
 
 import junit.framework.TestCase;
 
+import org.apache.commons.cli2.Argument;
 import org.apache.commons.cli2.Group;
 import org.apache.commons.cli2.OptionException;
 import org.apache.commons.cli2.builder.ArgumentBuilder;
 import org.apache.commons.cli2.builder.DefaultOptionBuilder;
 import org.apache.commons.cli2.builder.GroupBuilder;
 import org.apache.commons.cli2.commandline.Parser;
+import org.apache.commons.cli2.option.SourceDestArgument;
 
 /**
  * The first is a loop in Parser.parse() if I set a non-declared option. This 
@@ -54,4 +56,21 @@
             assertEquals("Unexpected -abcdef while processing ant",e.getMessage());
         }
     }
+    
+    public void testLoopingOptionLookAlike2() {
+        final ArgumentBuilder abuilder = new ArgumentBuilder();
+        final GroupBuilder gbuilder = new GroupBuilder();
+        final Argument inputfile_opt = abuilder.withName("input").withMinimum(1).withMaximum(1).create();
+        final Argument outputfile_opt = abuilder.withName("output").withMinimum(1).withMaximum(1).create();
+        final Argument targets = new SourceDestArgument(inputfile_opt, outputfile_opt);
+        final Group options = gbuilder.withOption(targets).create();
+        final Parser parser = new Parser();
+        parser.setGroup(options);
+        try {
+            parser.parse(new String[] { "testfile.txt", "testfile.txt", "testfile.txt", "testfile.txt"
});
+            fail("OptionException");
+        } catch (OptionException e) {
+            assertEquals("Unexpected testfile.txt while processing ", e.getMessage());
+        }
+    }    
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message