JDK-8255997 : (fs) NullPointerException not thrown when first argument to Path.of or Paths.get is null
  • Type: CSR
  • Component: core-libs
  • Sub-Component: java.nio
  • Priority: P4
  • Status: Closed
  • Resolution: Approved
  • Fix Versions: 16
  • Submitted: 2020-11-06
  • Updated: 2021-01-22
  • Resolved: 2020-12-17
Related Reports
CSR :  
Description
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.


Comments
I agree a release note is warranted here; retroactively moving to Approved.
17-12-2020

A release note is planned and the compatibility risk should be low because Path.of((Path) null) throws NPE, it's the only case where there are two or more parameters provided the first is null.
10-11-2020

This CSR is retroactive as the change to resolve the associated issue has already been integrated.
06-11-2020