commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Denis Haskin (JIRA)" <j...@apache.org>
Subject [jira] Created: (CLI-206) If Parser.parse is called with a Properties parameter that contains an option not defined, get a NPE
Date Thu, 15 Jul 2010 21:23:52 GMT
If Parser.parse is called with a Properties parameter that contains an option not defined,
get a NPE
----------------------------------------------------------------------------------------------------

                 Key: CLI-206
                 URL: https://issues.apache.org/jira/browse/CLI-206
             Project: Commons CLI
          Issue Type: Bug
          Components: Parser
    Affects Versions: 1.2
            Reporter: Denis Haskin


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);
    }
}


-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


Mime
View raw message