elharo opened a new issue, #379:
URL: https://github.com/apache/maven-shared-utils/issues/379
## Description
`StringUtils.chopNewline("")` throws `StringIndexOutOfBoundsException` when
called with an empty string.
## Location
`src/main/java/org/apache/maven/shared/utils/StringUtils.java:966-967`
## Root Cause
When `str` is the empty string `""`, `str.length() = 0`, so `lastIdx = -1`.
The call `str.charAt(lastIdx)` at line 967 immediately throws
`StringIndexOutOfBoundsException`.
## Steps to Reproduce
```java
StringUtils.chopNewline(""); // throws StringIndexOutOfBoundsException
```
## Suggested Fix
Add an early return for empty strings:
```java
public static String chopNewline(@NonNull String str) {
if (str.isEmpty()) {
return "";
}
int lastIdx = str.length() - 1;
...
}
```
Note: the `@NonNull` annotation on the parameter is also misleading since
the method also does not handle `null` input. If null handling is desired, a
null guard should be added as well.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]