1 /*
   2  * Copyright (c) 2016, 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="lists")
  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[][] lists() {
  67         final boolean MODIFIABLE = true;
  68         final boolean NON_MODIFIABLE = false;
  69         List<Integer> c = Arrays.asList(42);
  70 
  71         return new Object[][] {
  72             {c, NON_MODIFIABLE},
  73             {new ArrayList<>(c), MODIFIABLE},
  74             {new LinkedList<>(c), MODIFIABLE},
  75             {new MyList(), NON_MODIFIABLE},
  76             {new Vector<>(c), MODIFIABLE},
  77             {Collections.singletonList(42), NON_MODIFIABLE},
  78             {Collections.checkedList(c, Integer.class), NON_MODIFIABLE},
  79             {Collections.checkedList(new ArrayList<>(c), Integer.class), MODIFIABLE},
  80             {Collections.checkedList(new LinkedList<>(c), Integer.class), MODIFIABLE},
  81             {Collections.checkedList(new Vector<>(c), Integer.class), MODIFIABLE},
  82             {Collections.synchronizedList(c), NON_MODIFIABLE},
  83             {Collections.synchronizedList(new ArrayList<>(c)), MODIFIABLE},
  84             {Collections.synchronizedList(new LinkedList<>(c)), MODIFIABLE},
  85             {Collections.synchronizedList(new Vector<>(c)), MODIFIABLE},
  86             {Collections.unmodifiableList(c), NON_MODIFIABLE},
  87             {Collections.unmodifiableList(new ArrayList<>(c)), NON_MODIFIABLE},
  88             {Collections.unmodifiableList(new LinkedList<>(c)), NON_MODIFIABLE},
  89             {Collections.unmodifiableList(new Vector<>(c)), NON_MODIFIABLE},
  90         };
  91     }
  92 
  93     static class MyList extends AbstractList<Integer> {
  94         public Integer get(int index) { return 42; }
  95         public int size() { return 1; }
  96     }
  97 }