JDK-4491429 : HashMap regression with key value null
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util:collections
  • Affected Version: 1.4.0
  • Priority: P1
  • Status: Closed
  • Resolution: Fixed
  • OS:
    generic,linux,solaris_2.6,windows_nt generic,linux,solaris_2.6,windows_nt
  • CPU: generic,x86,sparc
  • Submitted: 2001-08-13
  • Updated: 2021-03-03
  • Resolved: 2001-08-15
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
1.4.0 beta2Fixed
Related Reports
Duplicate :  
Duplicate :  
Duplicate :  
Duplicate :  
Duplicate :  
Relates :  
Description
HashMap no longer correctly handles null as a key value. Consider the following code that stores an object with a key of null:

import java.util.HashMap;

public class HashMapTest {
    public static void main(String[] args) {
        HashMap hash = new HashMap();
        Object key = null;
        Object value = new Object();
        hash.put(key, value);
        System.out.println(hash.get(key));
    }
}

When run under 1.3.1, this prints something like "java.lang.Object@720eeb".
When run under the latest promoted build (b75) it prints "null".

HashMap uses a special internal key, NULL_KEY, to represent null as a key value. Whenever put() is called, it calls maskNull() on the input key to convert any null keys to NULL_KEY. The bug is that it forgets to use this masked value when it later calls addEntry() to add the entry to the hashtable. Instead, it is currently using the unmasked key. The method putForCreate() has the same problem.

Please see the suggested fix.

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: generic merlin-beta3 FIXED IN: merlin-beta2 merlin-beta3 INTEGRATED IN: merlin-beta2 merlin-beta3 VERIFIED IN: merlin-beta3
14-06-2004

EVALUATION yes.
11-06-2004

SUGGESTED FIX ------- HashMap.java ------- *** /tmp/t3e8.0 Mon Aug 13 13:53:50 2001 --- HashMap.java Mon Aug 13 13:53:25 2001 *************** *** 367,373 **** } modCount++; ! addEntry(hash, key, value, i); return null; } --- 367,373 ---- } modCount++; ! addEntry(hash, k, value, i); return null; } *************** *** 394,400 **** } } ! createEntry(hash, key, value, i); } void putAllForCreate(Map m) { --- 394,400 ---- } } ! createEntry(hash, k, value, i); } void putAllForCreate(Map m) {
11-06-2004