RFR: 8314236: Overflow in Collections.rotate [v8]

Stuart Marks smarks at openjdk.org
Tue Aug 29 03:06:12 UTC 2023


On Fri, 25 Aug 2023 13:20:54 GMT, Nikita Sakharin <duke at openjdk.org> wrote:

>> `Collections.rotate` method contains a bug. This method throws IndexOutOfBoundsException on arrays larger than $2^{30}$ elements. The way to reproduce:
>> 
>> final int size = (1 << 30) + 1;
>> final List<Byte> list = new ArrayList<>(size);
>> for (int i = 0; i < size; ++i)
>>     list.add((byte) 0);
>> Collections.rotate(list, size - 1);
>> 
>> Output:
>> ```Exception in thread "main" java.lang.IndexOutOfBoundsException: Index -2147483648 out of bounds for length 1073741825```
>> 
>> In that case private method `Collections.rotate1` will be called. And the line:
>> `i += distance;`
>> will cause overflow. I fixed this method and wrote a test for it.
>> 
>> I've signed the Oracle Contributor Agreement, but I don't have permission to raise a bug in the JDK Bug System.
>> 
>> Kindly ask you to raise a bug.
>
> Nikita Sakharin has updated the pull request with a new target base due to a merge or a rebase. The pull request now contains one commit:
> 
>   fix overflow in Collections.rotate and write tests

(The bot has already admonished you about avoiding rebasing. In practice merging works well and is helpful so that reviewers can see incremental changes.)

The additional test cases look good. Sorry I forgot to mention this previously: it would be good to have a comment in the test that says that the test is _only_ testing the index computations and not the actual rotation of elements.

Meanwhile, I'll pull in the changes from your branch and run them through our internal build system, which is more reliable (and comprehensive) than GHA.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/15270#issuecomment-1696698071


More information about the core-libs-dev mailing list