aherbert commented on PR #1065:
URL: https://github.com/apache/commons-lang/pull/1065#issuecomment-2030710084
I would close it. The javadoc currently reflects what the code is doing. If
you read the javadoc as is you should get the understanding that the method is
_trying_ to protect you from a mess up for the range of the index arguments.
However it does not stop real stupidity. You can do this and create a huge
array via negative overflow:
```java
int[] a = {0};
// Tries to create an array of size Integer.MAX_VALUE !
subarray(a, 1, Integer.MIN_VALUE);
```
A caller may soon find out this is a bad call due to either memory
allocation error, or an error from the subsequent arraycopy which could fail as
the source array is too small. If the source array happens to be huge as well
then you could end up copying something. But currently this method does not
protect from such bad arguments.
This method is from lang 2.1 (circa Nov 2005). Arrays.copyOfRange is in JDK
1.6 (circa Dec 2006) and does the copy correctly if you specify valid indices.
Since this is doing checks to make it null safe and index safe we wish to avoid
delegating to copyOfRange after we have clipped indices to avoid duplicating
index checks; we also wish to return a singleton empty array if possible. A fix
would be to remove the check on the size and do:
```java
if (endIndexExclusive <= startIndexInclusive) {
return // empty array
}
// Now compute size...
```
I think this would maintain the intention of the method. To allow you to
call with basically any argument and get either a null array, empty array or a
populated (sub-)array if you happen to have covered some/all of the range of
the input array with the arguments. If you remove this intention, then you may
as well just deprecate in favour of copyOfRange.
--
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]