Skip to content

Commit 260d465

Browse files
committed
8340572: ConcurrentModificationException when sorting ArrayList sublists
Reviewed-by: smarks
1 parent 9a25f82 commit 260d465

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

src/java.base/share/classes/java/util/ArrayList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1808,6 +1808,7 @@ private void replaceAllRange(UnaryOperator<E> operator, int i, int end) {
18081808
@Override
18091809
public void sort(Comparator<? super E> c) {
18101810
sortRange(c, 0, size);
1811+
modCount++;
18111812
}
18121813

18131814
@SuppressWarnings("unchecked")
@@ -1816,7 +1817,6 @@ private void sortRange(Comparator<? super E> c, int fromIndex, int toIndex) {
18161817
Arrays.sort((E[]) elementData, fromIndex, toIndex, c);
18171818
if (modCount != expectedModCount)
18181819
throw new ConcurrentModificationException();
1819-
modCount++;
18201820
}
18211821

18221822
void checkInvariants() {
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import java.util.ArrayList;
25+
import java.util.Collections;
26+
import java.util.ConcurrentModificationException;
27+
import java.util.List;
28+
29+
/**
30+
* @test
31+
* @bug 8340572
32+
* @summary ConcurrentModificationException when sorting ArrayList sublists
33+
*/
34+
35+
public class SortingModCount {
36+
public static void main(String[] args) {
37+
testSortingSubListsDoesNotIncrementModCount();
38+
testSortingListDoesIncrementModCount();
39+
}
40+
41+
private static void testSortingSubListsDoesNotIncrementModCount() {
42+
List<Integer> l = new ArrayList<>(List.of(1, 2, 3, 4));
43+
var a = l.subList(0, 2);
44+
var b = l.subList(2, 4);
45+
Collections.sort(a);
46+
Collections.sort(b);
47+
}
48+
49+
private static void testSortingListDoesIncrementModCount() {
50+
List<Integer> l = new ArrayList<>(List.of(1, 2, 3, 4));
51+
var b = l.subList(2, 4);
52+
Collections.sort(l);
53+
try {
54+
Collections.sort(b);
55+
throw new Error("expected ConcurrentModificationException not thrown");
56+
} catch (ConcurrentModificationException expected) {
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)