8163353: NPE in ConcurrentHashMap.removeAll()

Reviewed-by: martin, psandoz, redestad, alanb
This commit is contained in:
Doug Lea 2016-08-24 12:50:37 -07:00
parent 34565b8ada
commit 4c999ed180
2 changed files with 19 additions and 1 deletions

View File

@ -4566,7 +4566,10 @@ public class ConcurrentHashMap<K,V> extends AbstractMap<K,V>
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();

View File

@ -359,6 +359,21 @@ public class ConcurrentHashMapTest extends JSR166TestCase {
assertTrue(s.contains(five));
}
/**
* 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
*/