[jira] Created: (CLI-206) If Parser.parse is called with a Properties parameter that contains an option not defined, get a NPE

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

[jira] Created: (CLI-206) If Parser.parse is called with a Properties parameter that contains an option not defined, get a NPE

Alex D Herbert (Jira)
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.

Reply | Threaded
Open this post in threaded view
|

[jira] Updated: (CLI-206) If Parser.parse is called with a Properties parameter that contains an option not defined, get a NPE

Alex D Herbert (Jira)

     [ https://issues.apache.org/jira/browse/CLI-206?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Emmanuel Bourg updated CLI-206:
-------------------------------

    Fix Version/s: 1.3
      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:

{code:java}
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);
    }
}
{code}

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



> 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
>             Fix For: 1.3
>
>
> 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:
> {code:java}
> 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);
>     }
> }
> {code}

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

Reply | Threaded
Open this post in threaded view
|

[jira] Commented: (CLI-206) If Parser.parse is called with a Properties parameter that contains an option not defined, get a NPE

Alex D Herbert (Jira)
In reply to this post by Alex D Herbert (Jira)

    [ https://issues.apache.org/jira/browse/CLI-206?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12889039#action_12889039 ]

Emmanuel Bourg commented on CLI-206:
------------------------------------

Thank you for the detailed report. This issue has been fixed on the trunk, it's actually a duplicate of CLI-204.

> 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
>             Fix For: 1.3
>
>
> 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:
> {code:java}
> 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);
>     }
> }
> {code}

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

Reply | Threaded
Open this post in threaded view
|

[jira] Resolved: (CLI-206) If Parser.parse is called with a Properties parameter that contains an option not defined, get a NPE

Alex D Herbert (Jira)
In reply to this post by Alex D Herbert (Jira)

     [ https://issues.apache.org/jira/browse/CLI-206?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Emmanuel Bourg resolved CLI-206.
--------------------------------

    Resolution: Duplicate

> 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
>             Fix For: 1.3
>
>
> 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:
> {code:java}
> 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);
>     }
> }
> {code}

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