8163353: NPE in ConcurrentHashMap.removeAll()
authordl
Wed, 24 Aug 2016 12:50:37 -0700
changeset 40547 51759e487197
parent 40546 48249e95ef67
child 40548 5a9dbfa8e53e
8163353: NPE in ConcurrentHashMap.removeAll() Reviewed-by: martin, psandoz, redestad, alanb
jdk/src/java.base/share/classes/java/util/concurrent/ConcurrentHashMap.java
jdk/test/java/util/concurrent/tck/ConcurrentHashMapTest.java
--- a/jdk/src/java.base/share/classes/java/util/concurrent/ConcurrentHashMap.java	Wed Aug 24 11:23:58 2016 -0700
+++ b/jdk/src/java.base/share/classes/java/util/concurrent/ConcurrentHashMap.java	Wed Aug 24 12:50:37 2016 -0700
@@ -4566,7 +4566,10 @@
             boolean modified = false;
             // Use (c instanceof Set) as a hint that lookup in c is as
             // efficient as this view
-            if (c instanceof Set<?> && c.size() > map.table.length) {
+            Node<K,V>[] t;
+            if ((t = map.table) == null) {
+                return false;
+            } else if (c instanceof Set<?> && c.size() > t.length) {
                 for (Iterator<?> it = iterator(); it.hasNext(); ) {
                     if (c.contains(it.next())) {
                         it.remove();
--- a/jdk/test/java/util/concurrent/tck/ConcurrentHashMapTest.java	Wed Aug 24 11:23:58 2016 -0700
+++ b/jdk/test/java/util/concurrent/tck/ConcurrentHashMapTest.java	Wed Aug 24 12:50:37 2016 -0700
@@ -360,6 +360,21 @@
     }
 
     /**
+     * Test keySet().removeAll on empty map
+     */
+    public void testKeySet_empty_removeAll() {
+        ConcurrentHashMap<Integer, String> map = new ConcurrentHashMap<>();
+        Set<Integer> set = map.keySet();
+        set.removeAll(Collections.emptyList());
+        assertTrue(map.isEmpty());
+        assertTrue(set.isEmpty());
+        // following is test for JDK-8163353
+        set.removeAll(Collections.emptySet());
+        assertTrue(map.isEmpty());
+        assertTrue(set.isEmpty());
+    }
+
+    /**
      * keySet.toArray returns contains all keys
      */
     public void testKeySetToArray() {