Skip to content

Commit 57c08d0

Browse files
committed
Fix NPE in CommandLine.resolveOption(String).
1 parent 4384414 commit 57c08d0

3 files changed

Lines changed: 15 additions & 7 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@
4949
<action dev="ggregory" type="fix" due-to="Alexander Veit, Gary Gregory" issue="CLI-318">
5050
Inconsistent date format in changes report.
5151
</action>
52+
<action dev="ggregory" type="fix" due-to="Dilraj Singh, Gary Gregory" issue="CLI-283">
53+
Fix NPE in CommandLine.resolveOption(String).
54+
</action>
5255
<!-- ADD -->
5356
<action type="update" dev="ggregory" due-to="Dependabot, Gary Gregory">
5457
Add github/codeql-action.

src/main/java/org/apache/commons/cli/CommandLine.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -439,16 +439,17 @@ public Iterator<Option> iterator() {
439439
/**
440440
* Retrieves the option object given the long or short option as a String
441441
*
442-
* @param opt short or long name of the option.
442+
* @param opt short or long name of the option, may be null.
443443
* @return Canonicalized option.
444444
*/
445-
private Option resolveOption(String opt) {
446-
opt = Util.stripLeadingHyphens(opt);
447-
for (final Option option : options) {
448-
if (opt.equals(option.getOpt()) || opt.equals(option.getLongOpt())) {
449-
return option;
445+
private Option resolveOption(final String opt) {
446+
final String actual = Util.stripLeadingHyphens(opt);
447+
if (actual != null) {
448+
for (final Option option : options) {
449+
if (actual.equals(option.getOpt()) || actual.equals(option.getLongOpt())) {
450+
return option;
451+
}
450452
}
451-
452453
}
453454
return null;
454455
}

src/test/java/org/apache/commons/cli/bug/BugsTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static org.junit.Assert.assertEquals;
2121
import static org.junit.Assert.assertFalse;
2222
import static org.junit.Assert.assertNotNull;
23+
import static org.junit.Assert.assertNull;
2324
import static org.junit.Assert.assertTrue;
2425
import static org.junit.Assert.fail;
2526

@@ -135,6 +136,9 @@ public void test11680() throws Exception {
135136

136137
cmd.getOptionValue("f", "default f");
137138
cmd.getOptionValue("m", "default m");
139+
//
140+
assertNull(cmd.getOptionValue((String) null, null));
141+
assertEquals("default", cmd.getOptionValue((String) null, "default"));
138142
}
139143

140144
@Test

0 commit comments

Comments
 (0)