128 lines
4.4 KiB
Diff
128 lines
4.4 KiB
Diff
From 11bd45aac555a00c705c8bf480a179bb063b1c44 Mon Sep 17 00:00:00 2001
|
|
Subject: 8314236: Overflow in Collections.rotate
|
|
---
|
|
.../share/classes/java/util/Collections.java | 9 +-
|
|
.../java/util/Collections/RotateHuge.java | 85 +++++++++++++++++++
|
|
2 files changed, 90 insertions(+), 4 deletions(-)
|
|
create mode 100644 jdk/test/java/util/Collections/RotateHuge.java
|
|
|
|
diff --git a/jdk/src/share/classes/java/util/Collections.java b/jdk/src/share/classes/java/util/Collections.java
|
|
index 3ab4c5ec0..9aa373841 100644
|
|
--- a/jdk/src/share/classes/java/util/Collections.java
|
|
+++ b/jdk/src/share/classes/java/util/Collections.java
|
|
@@ -789,15 +789,16 @@ public class Collections {
|
|
if (distance == 0)
|
|
return;
|
|
|
|
- for (int cycleStart = 0, nMoved = 0; nMoved != size; cycleStart++) {
|
|
+ int bound = size - distance;
|
|
+ for (int cycleStart = 0, nMoved = 0; nMoved < size; cycleStart++) {
|
|
T displaced = list.get(cycleStart);
|
|
int i = cycleStart;
|
|
do {
|
|
- i += distance;
|
|
- if (i >= size)
|
|
+ if (i >= bound)
|
|
i -= size;
|
|
+ i += distance;
|
|
displaced = list.set(i, displaced);
|
|
- nMoved ++;
|
|
+ nMoved++;
|
|
} while (i != cycleStart);
|
|
}
|
|
}
|
|
diff --git a/jdk/test/java/util/Collections/RotateHuge.java b/jdk/test/java/util/Collections/RotateHuge.java
|
|
new file mode 100644
|
|
index 000000000..a6f8f739c
|
|
--- /dev/null
|
|
+++ b/jdk/test/java/util/Collections/RotateHuge.java
|
|
@@ -0,0 +1,85 @@
|
|
+/*
|
|
+ * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
|
|
+ * Copyright (c) 2023, Huawei Technologies Co., Ltd. All rights reserved.
|
|
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
+ *
|
|
+ * This code is free software; you can redistribute it and/or modify it
|
|
+ * under the terms of the GNU General Public License version 2 only, as
|
|
+ * published by the Free Software Foundation.
|
|
+ *
|
|
+ * This code is distributed in the hope that it will be useful, but WITHOUT
|
|
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
+ * version 2 for more details (a copy is included in the LICENSE file that
|
|
+ * accompanied this code).
|
|
+ *
|
|
+ * You should have received a copy of the GNU General Public License version
|
|
+ * 2 along with this work; if not, write to the Free Software Foundation,
|
|
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
+ *
|
|
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
+ * or visit www.oracle.com if you need additional information or have any
|
|
+ * questions.
|
|
+ */
|
|
+
|
|
+/*
|
|
+ * @test
|
|
+ * @bug 8314236
|
|
+ * @summary Overflow in Collections.rotate
|
|
+ */
|
|
+
|
|
+import com.sun.crypto.provider.Preconditions;
|
|
+
|
|
+import java.util.AbstractList;
|
|
+import java.util.Collections;
|
|
+import java.util.List;
|
|
+import java.util.RandomAccess;
|
|
+
|
|
+public class RotateHuge {
|
|
+
|
|
+ private static final class MockList extends AbstractList<Object>
|
|
+ implements RandomAccess {
|
|
+ private final int size;
|
|
+
|
|
+ public MockList(final int size) {
|
|
+ if (size < 0)
|
|
+ throw new IllegalArgumentException("Illegal size: " + size);
|
|
+ this.size = size;
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public Object get(final int index) {
|
|
+ Preconditions.checkIndex(index, size, null);
|
|
+ return null;
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public Object set(final int index, final Object element) {
|
|
+ Preconditions.checkIndex(index, size, null);
|
|
+ return null;
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public int size() {
|
|
+ return size;
|
|
+ }
|
|
+ }
|
|
+
|
|
+ public static void main(final String[] args) {
|
|
+ testRotate((1 << 30) + 1, -(1 << 30) - 2);
|
|
+ testRotate((1 << 30) + 1, 1 << 30);
|
|
+ testRotate(Integer.MAX_VALUE, Integer.MIN_VALUE);
|
|
+ testRotate(Integer.MAX_VALUE, Integer.MIN_VALUE + 3);
|
|
+ testRotate(Integer.MAX_VALUE, 2);
|
|
+ testRotate(Integer.MAX_VALUE, Integer.MAX_VALUE - 1);
|
|
+ }
|
|
+
|
|
+ /*
|
|
+ * This test covers only index computations.
|
|
+ * Correctness of elements rotation is not checked.
|
|
+ */
|
|
+ private static void testRotate(final int size, final int distance) {
|
|
+ final List<Object> list = new MockList(size);
|
|
+ Collections.rotate(list, distance);
|
|
+ }
|
|
+}
|
|
--
|
|
2.22.0
|
|
|