6691215: (coll) IdentityHashMap.containsValue(null) returns true when null value not present
authormartin
Sat, 10 May 2008 12:14:53 -0700
changeset 494 320ce398f07e
parent 493 b8102e80be10
child 496 6ce58b34b3df
6691215: (coll) IdentityHashMap.containsValue(null) returns true when null value not present Reviewed-by: dl, chegar, alanb Contributed-by: scottb@google.com
jdk/src/share/classes/java/util/IdentityHashMap.java
jdk/test/java/util/Collection/MOAT.java
--- a/jdk/src/share/classes/java/util/IdentityHashMap.java	Sat May 10 11:49:25 2008 -0700
+++ b/jdk/src/share/classes/java/util/IdentityHashMap.java	Sat May 10 12:14:53 2008 -0700
@@ -188,7 +188,6 @@
     /**
      * Use NULL_KEY for key if it is null.
      */
-
     private static Object maskNull(Object key) {
         return (key == null ? NULL_KEY : key);
     }
@@ -378,8 +377,8 @@
      */
     public boolean containsValue(Object value) {
         Object[] tab = table;
-        for (int i = 1; i < tab.length; i+= 2)
-            if (tab[i] == value)
+        for (int i = 1; i < tab.length; i += 2)
+            if (tab[i] == value && tab[i - 1] != null)
                 return true;
 
         return false;
@@ -905,7 +904,6 @@
      * view the first time this view is requested.  The view is stateless,
      * so there's no reason to create more than one.
      */
-
     private transient Set<Map.Entry<K,V>> entrySet = null;
 
     /**
--- a/jdk/test/java/util/Collection/MOAT.java	Sat May 10 11:49:25 2008 -0700
+++ b/jdk/test/java/util/Collection/MOAT.java	Sat May 10 12:14:53 2008 -0700
@@ -25,7 +25,7 @@
  * @test
  * @bug     6207984 6272521 6192552 6269713 6197726 6260652 5073546 4137464
  *          4155650 4216399 4294891 6282555 6318622 6355327 6383475 6420753
- *          6431845 4802633 6570566 6570575 6570631 6570924 6691185
+ *          6431845 4802633 6570566 6570575 6570631 6570924 6691185 6691215
  * @summary Run many tests on many Collection and Map implementations
  * @author  Martin Buchholz
  */
@@ -247,6 +247,13 @@
         testEmptySet(m.keySet());
         testEmptySet(m.entrySet());
         testEmptyCollection(m.values());
+
+        try { check(! m.containsValue(null)); }
+        catch (NullPointerException _) { /* OK */ }
+        try { check(! m.containsKey(null)); }
+        catch (NullPointerException _) { /* OK */ }
+        check(! m.containsValue(1));
+        check(! m.containsKey(1));
     }
 
     private static void testImmutableMap(final Map<Integer,Integer> m) {