Skip to content

Commit 4e0cdd0

Browse files
committed
[CLI-344] Option.processValue() throws NullPointerException when passed
null value with value separator configured Fail faster with a more precise NullPointerException
1 parent f9f6ab1 commit 4e0cdd0

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

src/changes/changes.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
<action type="fix" dev="ggregory" due-to="Gary Gregory">Remove -nouses directive from maven-bundle-plugin. OSGi package imports now state 'uses' definitions for package imports, this doesn't affect JPMS (from org.apache.commons:commons-parent:80).</action>
3636
<action type="fix" dev="ggregory" due-to="Arnout Engelen">Deprecate PatternOptionBuilder.PatternOptionBuilder().</action>
3737
<action type="fix" issue="CLI-341" dev="ggregory" due-to="Ruiqi Dong, Gary Gregory">HelpFormatter infinite loop with 0 width input.</action>
38-
<action type="fix" issue="CLI-349" dev="ggregory" due-to="Leo Fernandes, Gary Gregory">DefaultParser.parse() throws NullPointerException when options parameter is null.</action>
38+
<action type="fix" issue="CLI-349" dev="ggregory" due-to="Leo Fernandes, Gary Gregory">Fail faster with a more precise NullPointerException: Option.processValue() throws NullPointerException when passed null value with value separator configured.</action>
39+
<action type="fix" issue="CLI-344" dev="ggregory" due-to="Ruiqi Dong, Gary Gregory">Fail faster with a more precise NullPointerException: DefaultParser.parse() throws NullPointerException when options parameter is null.</action>
3940
<!-- ADD -->
4041
<action type="add" issue="CLI-339" dev="ggregory" due-to="Claude Warren, Gary Gregory">Help formatter extension in the new package #314.</action>
4142
<action type="add" dev="ggregory" due-to="Gary Gregory">CommandLine.Builder implements Supplier&lt;CommandLine&gt;.</action>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@ void processValue(final String value) {
830830
if (argCount == UNINITIALIZED) {
831831
throw new IllegalArgumentException("NO_ARGS_ALLOWED");
832832
}
833-
String add = value;
833+
String add = Objects.requireNonNull(value, "value");
834834
// this Option has a separator character
835835
if (hasValueSeparator()) {
836836
// get the separator character

src/test/java/org/apache/commons/cli/OptionTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,14 @@ void testHashCode() {
300300
assertNotEquals(Option.builder("test").build().hashCode(), Option.builder("test").longOpt("long test").build().hashCode());
301301
}
302302

303+
@Test
304+
public void testProcessValue() {
305+
final Option option = new Option("D", true, "Define property");
306+
option.setValueSeparator('=');
307+
final NullPointerException exception = assertThrows(NullPointerException.class, () -> option.processValue(null));
308+
assertTrue(exception.getMessage().contains("value"));
309+
}
310+
303311
@Test
304312
void testSerialization() throws IOException, ClassNotFoundException {
305313
final Option option = Option.builder("o").type(TypeHandlerTest.Instantiable.class).build();

0 commit comments

Comments
 (0)