Uploaded image for project: 'Commons CLI'
  1. Commons CLI
  2. CLI-206

If Parser.parse is called with a Properties parameter that contains an option not defined, get a NPE

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Duplicate
    • Affects Version/s: 1.2
    • Fix Version/s: 1.3
    • Component/s: Parser
    • Labels:
      None

      Description

      See junit test below.

      The problems appears to be in org.apache.commons.cli.Parser.processProperties, around lines 252 - 259.

      It's trying to get the Option object for the property's name, but if none was specified, then opt is null and the attempt to call opt.hasArg() at line 259 throws an NPE.

      The fix should be to skip the remainder of the clause if the Option object is null.

      Example unit test:

      package test;
      
      import java.util.Properties;
      
      import org.apache.commons.cli.OptionBuilder;
      import org.apache.commons.cli.Options;
      import org.apache.commons.cli.PosixParser;
      import org.junit.Test;
      
      
      public class CommonsCliBugTest {
      
          @Test
          public void testCommonsCliOkay() throws Exception {
              String[] args = new String[] {};
              Properties p = new Properties();
              p.setProperty("alpha", "beta");
              Options options = new Options();
              options.addOption(OptionBuilder.withLongOpt("alpha").hasArg().withArgName("ALPHA").create());
              PosixParser parser = new PosixParser();
              parser.parse(options, args, p);
          }
      
          @Test
          public void testCommonsCliFails() throws Exception {
              String[] args = new String[] {};
              Properties p = new Properties();
              // Note this property is not specified as a valid option
              p.setProperty("gamma", "beta");
              Options options = new Options();
              options.addOption(OptionBuilder.withLongOpt("alpha").hasArg().withArgName("ALPHA").create());
              PosixParser parser = new PosixParser();
              parser.parse(options, args, p);
          }
      }
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                dwhsix Denis Haskin
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: