You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2026/03/28 19:32:24 UTC

(commons-collections) branch master updated: [COLLECTIONS-887] ConcurrentReferenceHashMap.remove(key), remove(key, value), replace(key, value), and replace(key, oldValue, newValue) throw NullPointerException inconsistently

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-collections.git


The following commit(s) were added to refs/heads/master by this push:
     new 0f0abefb6 [COLLECTIONS-887] ConcurrentReferenceHashMap.remove(key), remove(key, value), replace(key, value), and replace(key, oldValue, newValue) throw NullPointerException inconsistently
0f0abefb6 is described below

commit 0f0abefb68ad4475f6fc6a7b7e9326a0d5f535d7
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Mar 28 15:32:18 2026 -0400

    [COLLECTIONS-887] ConcurrentReferenceHashMap.remove(key), remove(key,
    value), replace(key, value), and replace(key, oldValue, newValue) throw
    NullPointerException inconsistently
---
 src/changes/changes.xml                            |  2 +
 .../map/ConcurrentReferenceHashMap.java            |  9 ++
 .../AbstractConcurrentReferenceHashMapTest.java    | 99 ++++++++++++++++++++++
 3 files changed, 110 insertions(+)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index c33c075b1..0d3b12cad 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -44,6 +44,8 @@
     <action type="fix" dev="ggregory" due-to="Paul King, Gary Gregory, Sebb">Add MultiValuedMap.inverted() #665.</action>
     <action type="fix" dev="ggregory" due-to="John Griffin, Gary Gregory" issue="COLLECTIONS-885">PatriciaTrie submap and range view operations silently mutate trie structure resulting in ConcurrentModificationException for any iterating threads #672.</action>
     <action type="fix" dev="ggregory" due-to="Partha Protim Paul" issue="COLLECTIONS-886">[javadoc] CompositeSet.addComposited(Set) does not throw NullPointerException when null is passed #675.</action>
+    <action type="fix" dev="ggregory" due-to="Partha Paul, Gary Gregory" issue="COLLECTIONS-887">ConcurrentReferenceHashMap.remove(key), remove(key, value), replace(key, value), and replace(key, oldValue, newValue) throw NullPointerException inconsistently
+.</action>
     <!-- ADD -->
     <action type="add" dev="ggregory" due-to="Gary Gregory">Add generics to UnmodifiableIterator for the wrapped type.</action>
     <action type="add" dev="ggregory" due-to="Gary Gregory">Add a Maven benchmark profile for JMH.</action>
diff --git a/src/main/java/org/apache/commons/collections4/map/ConcurrentReferenceHashMap.java b/src/main/java/org/apache/commons/collections4/map/ConcurrentReferenceHashMap.java
index c7cdea395..6ae529102 100644
--- a/src/main/java/org/apache/commons/collections4/map/ConcurrentReferenceHashMap.java
+++ b/src/main/java/org/apache/commons/collections4/map/ConcurrentReferenceHashMap.java
@@ -1642,7 +1642,16 @@ public class ConcurrentReferenceHashMap<K, V> extends AbstractMap<K, V> implemen
         return segmentFor(hash).get(key, hash);
     }
 
+    /**
+     * Returns the hash code of the given key, which is either the result of calling {@code hashCode} or {@code System.identityHashCode} depending on
+     * {@code identityComparisons}.
+     * 
+     * @param key The key to hash.
+     * @return the hash code of the given key.
+     * @throws NullPointerException if the specified key is null.
+     */
     private int hashOf(final Object key) {
+        Objects.requireNonNull(key, "key");
         return hash(identityComparisons ? System.identityHashCode(key) : key.hashCode());
     }
 
diff --git a/src/test/java/org/apache/commons/collections4/map/AbstractConcurrentReferenceHashMapTest.java b/src/test/java/org/apache/commons/collections4/map/AbstractConcurrentReferenceHashMapTest.java
index 49447023f..8e33cb295 100644
--- a/src/test/java/org/apache/commons/collections4/map/AbstractConcurrentReferenceHashMapTest.java
+++ b/src/test/java/org/apache/commons/collections4/map/AbstractConcurrentReferenceHashMapTest.java
@@ -17,9 +17,13 @@
 
 package org.apache.commons.collections4.map;
 
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
 import java.util.EnumSet;
 
 import org.apache.commons.collections4.map.ConcurrentReferenceHashMap.Option;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
 
 /**
  * Tests {@link ConcurrentReferenceHashMap}.
@@ -31,6 +35,11 @@ public abstract class AbstractConcurrentReferenceHashMapTest<K, V> extends Abstr
 
     protected static final EnumSet<Option> IDENTITY_COMPARISONS = EnumSet.of(Option.IDENTITY_COMPARISONS);
 
+    @BeforeEach
+    void beforeEachFill() {
+        resetFull();
+    }
+
     @Override
     public boolean isAllowNullKey() {
         return false;
@@ -51,4 +60,94 @@ public abstract class AbstractConcurrentReferenceHashMapTest<K, V> extends Abstr
         return false;
     }
 
+    @Test
+    void testComputeNullKey() {
+        final ConcurrentReferenceHashMap<K, V> map = getMap();
+        assertThrows(NullPointerException.class, () -> map.compute(null, (k, v) -> getSampleValues()[0]));
+    }
+
+    @Test
+    void testComputeNullKeyIfAbsent() {
+        final ConcurrentReferenceHashMap<K, V> map = getMap();
+        assertThrows(NullPointerException.class, () -> map.computeIfAbsent(null, k -> getSampleValues()[0]));
+    }
+
+    @Test
+    void testComputeNullKeyIfPresent() {
+        final ConcurrentReferenceHashMap<K, V> map = getMap();
+        assertThrows(NullPointerException.class, () -> map.computeIfPresent(null, (k, v) -> getSampleValues()[0]));
+    }
+
+    @Test
+    void testContainsNullKey() {
+        final ConcurrentReferenceHashMap<K, V> map = getMap();
+        assertThrows(NullPointerException.class, () -> map.containsKey(null));
+    }
+
+    @Test
+    void testContainsNullValue() {
+        final ConcurrentReferenceHashMap<K, V> map = getMap();
+        assertThrows(NullPointerException.class, () -> map.containsValue(null));
+    }
+
+    @Test
+    void testGetNullKey() {
+        final ConcurrentReferenceHashMap<K, V> map = getMap();
+        assertThrows(NullPointerException.class, () -> map.get(null));
+    }
+
+    @Test
+    void testGetNullKeyOrDefault() {
+        final ConcurrentReferenceHashMap<K, V> map = getMap();
+        assertThrows(NullPointerException.class, () -> map.getOrDefault(null, getSampleValues()[0]));
+    }
+
+    @Test
+    void testMergeNullKey() {
+        final ConcurrentReferenceHashMap<K, V> map = getMap();
+        final V[] sampleValues = getSampleValues();
+        assertThrows(NullPointerException.class, () -> map.merge(null, sampleValues[0], (k, v) -> sampleValues[0]));
+    }
+
+    @Test
+    void testMergeNullValue() {
+        final ConcurrentReferenceHashMap<K, V> map = getMap();
+        assertThrows(NullPointerException.class, () -> map.merge(getSampleKeys()[0], null, (k, v) -> null));
+        map.merge(getSampleKeys()[0], getSampleValues()[0], (k, v) -> null);
+    }
+
+    @Test
+    void testPutNullKey() {
+        final ConcurrentReferenceHashMap<K, V> map = getMap();
+        final K key = null;
+        assertThrows(NullPointerException.class, () -> map.put(key, getSampleValues()[0]));
+    }
+
+    @Test
+    void testPutNullValue() {
+        final ConcurrentReferenceHashMap<K, V> map = getMap();
+        assertThrows(NullPointerException.class, () -> map.put(getSampleKeys()[0], null));
+    }
+
+    @Test
+    void testRemoveNullKey() {
+        final ConcurrentReferenceHashMap<K, V> map = getMap();
+        final K key = null;
+        final V[] values = getSampleValues();
+        assertThrows(NullPointerException.class, () -> map.remove(key, values[0]));
+        assertThrows(NullPointerException.class, () -> map.remove(key));
+        assertThrows(NullPointerException.class, () -> map.replace(key, values[0]));
+        assertThrows(NullPointerException.class, () -> map.replace(key, values[0], values[1]));
+    }
+
+    @Test
+    void testReplaceNullKey() {
+        final ConcurrentReferenceHashMap<K, V> map = getMap();
+        final K key = null;
+        final V[] values = getSampleValues();
+        assertThrows(NullPointerException.class, () -> map.remove(key, values[0]));
+        assertThrows(NullPointerException.class, () -> map.remove(key));
+        assertThrows(NullPointerException.class, () -> map.replace(key, values[0]));
+        assertThrows(NullPointerException.class, () -> map.replace(key, values[0], values[1]));
+    }
 }