8173706: Is able to set a negative j.u.Vector size in JDK9 b151
authordl
Fri, 03 Feb 2017 13:24:59 -0800
changeset 43520 65d31ea930aa
parent 43519 7b7f79ae8f29
child 43521 60e247b8d9a4
8173706: Is able to set a negative j.u.Vector size in JDK9 b151 Reviewed-by: martin, psandoz, smarks
jdk/src/java.base/share/classes/java/util/Vector.java
jdk/test/java/util/concurrent/tck/VectorTest.java
--- a/jdk/src/java.base/share/classes/java/util/Vector.java	Fri Feb 03 08:15:55 2017 -0800
+++ b/jdk/src/java.base/share/classes/java/util/Vector.java	Fri Feb 03 13:24:59 2017 -0800
@@ -307,8 +307,9 @@
         if (newSize > elementData.length)
             grow(newSize);
         final Object[] es = elementData;
-        for (int to = elementCount, i = elementCount = newSize; i < to; i++)
+        for (int to = elementCount, i = newSize; i < to; i++)
             es[i] = null;
+        elementCount = newSize;
     }
 
     /**
@@ -1443,9 +1444,8 @@
 
         @SuppressWarnings("unchecked")
         public boolean tryAdvance(Consumer<? super E> action) {
+            Objects.requireNonNull(action);
             int i;
-            if (action == null)
-                throw new NullPointerException();
             if (getFence() > (i = index)) {
                 index = i + 1;
                 action.accept((E)array[i]);
@@ -1458,8 +1458,7 @@
 
         @SuppressWarnings("unchecked")
         public void forEachRemaining(Consumer<? super E> action) {
-            if (action == null)
-                throw new NullPointerException();
+            Objects.requireNonNull(action);
             final int hi = getFence();
             final Object[] a = array;
             int i;
--- a/jdk/test/java/util/concurrent/tck/VectorTest.java	Fri Feb 03 08:15:55 2017 -0800
+++ b/jdk/test/java/util/concurrent/tck/VectorTest.java	Fri Feb 03 13:24:59 2017 -0800
@@ -67,12 +67,18 @@
      * tests for setSize()
      */
     public void testSetSize() {
-        Vector v = new Vector();
+        final Vector v = new Vector();
         for (int n : new int[] { 100, 5, 50 }) {
             v.setSize(n);
             assertEquals(n, v.size());
             assertNull(v.get(0));
             assertNull(v.get(n - 1));
+            assertThrows(
+                    ArrayIndexOutOfBoundsException.class,
+                    new Runnable() { public void run() { v.setSize(-1); }});
+            assertEquals(n, v.size());
+            assertNull(v.get(0));
+            assertNull(v.get(n - 1));
         }
     }