TreeSet and TreeMap.keySet() empty subSet spliterator does not throw NullPointerException

XMLWordPrintable

    • Type: CSR
    • Resolution: Unresolved
    • Priority: P4
    • 27
    • Component/s: core-libs
    • None
    • behavioral
    • low
    • The new NPE only happens to an empty subSet, existing code would have ran into NPE for non-empty subSets before accidentally relying on this behavior.
    • Java API
    • Implementation

      Summary

      For the subSet from a java.util.TreeSet or a java.util.TreeMap::keySet, if such a set is empty, its spliterator now properly throws NullPointerException for tryAdvance and forEachRemaining.

      Problem

      The subSet for a TreeSet or a TreeMap.entrySet() got a customized spliterator implementation in JDK-8011426. That patch incorrectly omitted null checks when the subSet is empty, because the arguments are unused for empty subSets.

      For example, in JShell:

      new TreeSet<Integer>().subSet(-1, 1).spliterator().tryAdvance(null)
      

      returns false instead of throwing a NullPointerException.

      Solution

      Throw a NullPointerException instead of returning false for tryAdvance or return successfully for forEachRemaining for the spliterator of such empty subsets.

      Specification

      No change. This is a behavioral change that aligns implementation to specification.

            Assignee:
            Chen Liang
            Reporter:
            Oli Gillespie
            Roger Riggs
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: