1 /* 2 * Copyright (c) 2015, 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 /* 25 * @test 26 * @bug 8079136 27 * @run testng NestedSubList 28 * @summary Accessing a nested sublist leads to StackOverflowError 29 */ 30 31 import java.util.AbstractList; 32 import java.util.Arrays; 33 import java.util.ArrayList; 34 import java.util.Collections; 35 import java.util.LinkedList; 36 import java.util.List; 37 import java.util.Vector; 38 39 import org.testng.annotations.Test; 40 import org.testng.annotations.DataProvider; 41 import static org.testng.Assert.fail; 42 43 public class NestedSubList { 44 45 static final int NEST_LIMIT = 65536; 46 47 @Test(dataProvider="sublists") 48 public void testAccessToSublists(List<Integer> list, boolean modifiable) { 49 Class<?> cls = list.getClass(); 50 for (int i = 0; i < NEST_LIMIT; ++i) { 51 list = list.subList(0, 1); 52 } 53 54 try { 55 list.get(0); 56 if (modifiable) { 57 list.remove(0); 58 list.add(0, 42); 59 } 60 } catch (StackOverflowError e) { 61 fail("failed for " + cls); 62 } 63 } 64 65 @DataProvider 66 public static Object[][] sublists() { 67 List<Integer> c = Arrays.asList(42); 68 69 return new Object[][] { 70 {c, false}, 71 {new ArrayList<>(c), true}, 72 {new LinkedList<>(c), true}, 73 {new MyList(), false}, 74 {new Vector<>(c), true}, 75 {Collections.singletonList(42), false}, 76 {Collections.checkedList(c, Integer.class), false}, 77 {Collections.checkedList(new ArrayList<>(c), Integer.class), true}, 78 {Collections.checkedList(new LinkedList<>(c), Integer.class), true}, 79 {Collections.checkedList(new Vector<>(c), Integer.class), true}, 80 {Collections.synchronizedList(c), false}, 81 {Collections.synchronizedList(new ArrayList<>(c)), true}, 82 {Collections.synchronizedList(new LinkedList<>(c)), true}, 83 {Collections.synchronizedList(new Vector<>(c)), true}, 84 {Collections.unmodifiableList(c), false}, 85 {Collections.unmodifiableList(new ArrayList<>(c)), false}, 86 {Collections.unmodifiableList(new LinkedList<>(c)), false}, 87 {Collections.unmodifiableList(new Vector<>(c)), false}, 88 }; 89 } 90 91 static class MyList extends AbstractList<Integer> { 92 public Integer get(int index) { return 42; } 93 public int size() { return 1; } 94 } 95 }