Skip to content

Commit ae71470

Browse files
committed
Avoid NullPointerException in Util.stripLeadingAndTrailingQuotes(String)
1 parent ec17b87 commit ae71470

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@
2424
<body>
2525
<release version="1.7.0" date="202Y-MM-DD" description="New features and bug fixes">
2626
<!-- FIX -->
27-
<action type="fix" issue="CLI-312" dev="ggregory" due-to="Claude Warren, Gary Gregory">Inconsistent behaviour in key/value pairs (Java property style).</action>
27+
<action type="fix" issue="CLI-312" dev="ggregory" due-to="Claude Warren, Gary Gregory">Inconsistent behavior in key/value pairs (Java property style).</action>
28+
<action type="fix" dev="ggregory" due-to="Gary Gregory">Avoid NullPointerException in Util.stripLeadingAndTrailingQuotes(String).</action>
2829
<!-- ADD -->
2930
<action type="add" issue="CLI-321" dev="ggregory" due-to="Claude Warren, Gary Gregory">Add and use a Converter interface and implementations without using BeanUtils #216.</action>
3031
<action type="add" dev="ggregory" due-to="Gary Gregory">Add Maven property project.build.outputTimestamp for build reproducibility.</action>
3132
<action type="add" issue="CLI-322" dev="ggregory" due-to="Claude Warren, Gary Gregory">Add '-' as an option char and implemented extensive tests #217.</action>
3233
<action type="add" issue="CLI-324" dev="ggregory" due-to="Claude Warren, Gary Gregory">Make adding OptionGroups and Options to existing Options easier #230.</action>
33-
<action type="add" issue="CLI-323" dev="ggregory" due-to="Claude Warren, Gary Gregory">Added Supplier<T> defaults for getParsedOptionValue #229 .</action>
34+
<action type="add" issue="CLI-323" dev="ggregory" due-to="Claude Warren, Gary Gregory">Added Supplier&lt;T&gt; defaults for getParsedOptionValue #229 .</action>
3435
<!-- UPDATE -->
3536
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump commons-parent from 64 to 66.</action>
3637
</release>

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,26 @@ final class Util {
2727
*/
2828
static final String[] EMPTY_STRING_ARRAY = {};
2929

30+
/**
31+
* Tests whether the given string is null or empty.
32+
*
33+
* @param str The string to test.
34+
* @return Whether the given string is null or empty.
35+
*/
36+
private static boolean isEmpty(final String str) {
37+
return str == null || str.isEmpty();
38+
}
39+
3040
/**
3141
* Removes the leading and trailing quotes from {@code str}. E.g. if str is '"one two"', then 'one two' is returned.
3242
*
3343
* @param str The string from which the leading and trailing quotes should be removed.
3444
* @return The string without the leading and trailing quotes.
3545
*/
3646
static String stripLeadingAndTrailingQuotes(final String str) {
47+
if (isEmpty(str)) {
48+
return str;
49+
}
3750
final int length = str.length();
3851
if (length > 1 && str.startsWith("\"") && str.endsWith("\"") && str.substring(1, length - 1).indexOf('"') == -1) {
3952
return str.substring(1, length - 1);
@@ -48,8 +61,8 @@ static String stripLeadingAndTrailingQuotes(final String str) {
4861
* @return the new String.
4962
*/
5063
static String stripLeadingHyphens(final String str) {
51-
if (str == null) {
52-
return null;
64+
if (isEmpty(str)) {
65+
return str;
5366
}
5467
if (str.startsWith("--")) {
5568
return str.substring(2);

0 commit comments

Comments
 (0)