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

Nikita Sakharin duke at openjdk.org
Tue Aug 15 11:25:46 UTC 2023


> `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 incrementally with one additional commit since the last revision:

  8314236: fix style, add test requirements

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

Changes:
  - all: https://git.openjdk.org/jdk/pull/15270/files
  - new: https://git.openjdk.org/jdk/pull/15270/files/10bddbb9..dda1c3b0

Webrevs:
 - full: https://webrevs.openjdk.org/?repo=jdk&pr=15270&range=02
 - incr: https://webrevs.openjdk.org/?repo=jdk&pr=15270&range=01-02

  Stats: 11 lines in 2 files changed: 4 ins; 0 del; 7 mod
  Patch: https://git.openjdk.org/jdk/pull/15270.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/15270/head:pull/15270

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


More information about the core-libs-dev mailing list