1 Star 0 Fork 82

MYX/openjdk-1.8.0

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
8314236-Overflow-in-Collections.rotate.patch 4.40 KB
一键复制 编辑 原始数据 按行查看 历史
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
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/myx_076/openjdk-1.8.0.git
git@gitee.com:myx_076/openjdk-1.8.0.git
myx_076
openjdk-1.8.0
openjdk-1.8.0
master

搜索帮助