On Mon, 14 Aug 2023 12:56:46 GMT, Nikita Sakharin <[email protected]> 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.

This pull request has now been integrated.

Changeset: 3828dc91
Author:    Nikita Sakharin <[email protected]>
Committer: Aleksey Shipilev <[email protected]>
URL:       
https://git.openjdk.org/jdk/commit/3828dc913a3ea28d622b69bd07f26949128eb5f7
Stats:     89 lines in 2 files changed: 85 ins; 1 del; 3 mod

8314236: Overflow in Collections.rotate

Co-authored-by: Nikita Sakharin 
<[email protected]>
Reviewed-by: shade, smarks

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

PR: https://git.openjdk.org/jdk/pull/15270

Reply via email to