Aryan Ganjoo created COLLECTIONS-901:
----------------------------------------
Summary: ListUtils.partition last partition overflows when a list
is near Integer.MAX_VALUE
Key: COLLECTIONS-901
URL: https://issues.apache.org/jira/browse/COLLECTIONS-901
Project: Commons Collections
Issue Type: Bug
Reporter: Aryan Ganjoo
On current Commons Collections master
(2d985c27e07bdcbb9516aea44b0dda3f3e487ced), ListUtils.partition() can compute a
negative end index for the final partition of a very large virtual List.
Partition.get() evaluates start + size in int arithmetic before Math.min, so
the addition overflows even though both start and the underlying list size are
valid.
Reproduction without allocating a large list:
- Use an AbstractList<Integer> whose size() returns Integer.MAX_VALUE and whose
get(index) returns index.
- Call ListUtils.partition(list, (1 << 30) + 1).get(1).
- The old code computes start = 1073741825 and end = -2147483646, then subList
throws IllegalArgumentException: fromIndex(1073741825) > toIndex(-2147483646).
Expected: the second partition spans indices 1073741825 through 2147483646 and
has size 1073741822. A local one-line arithmetic correction and regression test
reproduce the old failure and pass after the fix. Independent exact-patch
review approved it; the default Maven build is being checked separately. No PR
or hosted CI result exists yet.
This differs from COLLECTIONS-673, which fixed overflow in Partition.size();
this failure is in Partition.get().
Prepared with AI assistance.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)