Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8046085

HashMap.put with null key may throw NullPointerException

    Details

    • Subcomponent:
    • Resolved In Build:
      b19
    • CPU:
      generic
    • OS:
      generic
    • Verification:
      Verified

      Backports

        Description

        FULL PRODUCT VERSION :
        java version "1.8.0_05"
        Java(TM) SE Runtime Environment (build 1.8.0_05-b13)
        Java HotSpot(TM) Client VM (build 25.5-b02, mixed mode, sharing)

        ADDITIONAL OS VERSION INFORMATION :
        Microsoft Windows [Version 6.1.7601]

        A DESCRIPTION OF THE PROBLEM :
        Changes to improve HashMap performance in Java 8 can cause HashMap.put() with a null key to throw a NullPointerException. This problem did not occur in Java 7.

        Caused by: java.lang.NullPointerException
            at java.util.HashMap$TreeNode.putTreeVal(HashMap.java:1970)
            at java.util.HashMap.putVal(HashMap.java:637)
            at java.util.HashMap.put(HashMap.java:611)

        It looks like the line of code in question is
        else if ((pk = p.key) == k || (pk != null && k.equals(pk)))

        Probably k.equals(pk) is source of the NullPointerException where k is null.
        Possibly fix is change it to pk.equals(k).


        REGRESSION. Last worked in version 7u17

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Problem occurs on continuous build machine during unit test. Unit test is parsing XML and custom ContentHandler is saving data to a HashMap.

        /* HashMap for saving parsed data */
            private static final ThreadLocal contextMap = new ThreadLocal() {
                protected Object initialValue() {
                    return new HashMap();
                }
            };

        /* Insertion of data into HashMap */
            Map map = (Map) contextMap.get();
            map.put(key, value);

        Problem has not occurred on development machine with limited testing, but consistently fails on build machine.


        ERROR MESSAGES/STACK TRACES THAT OCCUR :
        Caused by: java.lang.NullPointerException
            at java.util.HashMap$TreeNode.putTreeVal(HashMap.java:1970)
            at java.util.HashMap.putVal(HashMap.java:637)
            at java.util.HashMap.put(HashMap.java:611)


        REPRODUCIBILITY :
        This bug can be reproduced often.

        CUSTOMER SUBMITTED WORKAROUND :
        Check if key or value is not null, then insert into map.

          Activity

          Hide
          psandoz Paul Sandoz added a comment -
          This occurs when the hash of a key maps to a tree in the entry table, rather than a linked list i.e. when there are a high number of collisions.

          I think there was most likely a typo, the code at line 1970 in java.util.HashMap$TreeNode.putTreeVal should be:

                          else if ((pk = p.key) == k || (k != null && k.equals(pk)))

          All other relevant equality code was checked and looks correct.

          Better null key tests are required.
          Show
          psandoz Paul Sandoz added a comment - This occurs when the hash of a key maps to a tree in the entry table, rather than a linked list i.e. when there are a high number of collisions. I think there was most likely a typo, the code at line 1970 in java.util.HashMap$TreeNode.putTreeVal should be:                 else if ((pk = p.key) == k || (k != null && k.equals(pk))) All other relevant equality code was checked and looks correct. Better null key tests are required.
          Hide
          alanb Alan Bateman added a comment - - edited
          I agree with Paul's comment. The find method appears to be right, it seems to be just putTreeVal where it checks pk instead of k.

          This one is important to fix in the next 8 update if we can. It needs to be fixed in 9 first of course.
          Show
          alanb Alan Bateman added a comment - - edited I agree with Paul's comment. The find method appears to be right, it seems to be just putTreeVal where it checks pk instead of k. This one is important to fix in the next 8 update if we can. It needs to be fixed in 9 first of course.
          Hide
          dl Doug Lea added a comment -
          Paul: Thanks; yes, definitely a typo..
          Show
          dl Doug Lea added a comment - Paul: Thanks; yes, definitely a typo..
          Hide
          mduigou Michael Duigou added a comment -
          I will propose the obvious patch today along with a test.
          Show
          mduigou Michael Duigou added a comment - I will propose the obvious patch today along with a test.
          Hide
          mduigou Michael Duigou added a comment -
          webrev posted http://cr.openjdk.java.net/~mduigou/JDK-8046085/0/webrev/
          RFR posted to corelibs-dev
          Show
          mduigou Michael Duigou added a comment - webrev posted http://cr.openjdk.java.net/~mduigou/JDK-8046085/0/webrev/ RFR posted to corelibs-dev
          Hide
          hgupdate HG Updates added a comment -
          URL: http://hg.openjdk.java.net/jdk9/dev/jdk/rev/c9fe43f1cddb
          User: mduigou
          Date: 2014-06-06 21:44:24 +0000
          Show
          hgupdate HG Updates added a comment - URL: http://hg.openjdk.java.net/jdk9/dev/jdk/rev/c9fe43f1cddb User: mduigou Date: 2014-06-06 21:44:24 +0000
          Hide
          hgupdate HG Updates added a comment -
          URL: http://hg.openjdk.java.net/jdk9/jdk9/jdk/rev/c9fe43f1cddb
          User: lana
          Date: 2014-06-18 17:19:31 +0000
          Show
          hgupdate HG Updates added a comment - URL: http://hg.openjdk.java.net/jdk9/jdk9/jdk/rev/c9fe43f1cddb User: lana Date: 2014-06-18 17:19:31 +0000
          Hide
          idergali Ilya Dergalin (Inactive) added a comment -
          HashMap.java source is the same in JDK 8 and 8u5 so resetting affected version to 8.
          Show
          idergali Ilya Dergalin (Inactive) added a comment - HashMap.java source is the same in JDK 8 and 8u5 so resetting affected version to 8.

            People

            • Assignee:
              mduigou Michael Duigou
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              1 Vote for this issue
              Watchers:
              8 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: