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

(fs) NullPointerException not thrown when first argument to Path.of or Paths.get is null

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 16
    • core-libs
    • None
    • behavioral
    • low
    • It is possible that some code which incorrectly passes null as the first parameter to either of the methods in question could fail due to neglecting to account for a potential NullPointerException.
    • Java API
    • JDK

      Summary

      Modify implementation such that java.nio.file.Path.of(String first, String... more) and java.nio.file.Paths.get(String first, String... more) throw a NullPointerException when first is null.

      Problem

      The package specification of java.nio.file states that

      Unless otherwise noted, passing a null argument to a constructor or method of any class or interface in this package will cause a NullPointerException to be thrown.

      but the aforementioned methods do not throw a NullPointerException. This is incorrect behavior not only due to failing to throw the exception, but more in terms of allowing a corrupted path name containing the string "null" to be created. This could lead to unexpected and possibly confusing behavior.

      Solution

      Modify the implementations of getPath(String first, String... more) in the UnixFileSystem and WindowsFileSystem classes in the internal sun.nio.fs package such that they throw NullPointerException if the first parameter first is null. Paths.get calls Path.of() which calls XFileSystem.getPath().

      Specification

      There is no change to the existing specification per se.

            bpb Brian Burkhalter
            pnarayanaswa Praveen Narayanaswamy
            Alan Bateman
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: