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

File::getCanonicalFile doesn't work for \\?\C:\ style paths DOS device paths

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 22
    • core-libs
    • None
    • behavioral
    • medium
    • Hide
      There is some risk that File::toString is used to create a string that is passed to native code that expects the long path prefix. If this happens then the native code will need to fixed to prepend the long path prefix.

      The behaviour of several java.io.File methods may be observed to be different to older JDK releases. These differences are mostly that the methods work correctly when previously they yield incorrect results or failed.
      Show
      There is some risk that File::toString is used to create a string that is passed to native code that expects the long path prefix. If this happens then the native code will need to fixed to prepend the long path prefix. The behaviour of several java.io.File methods may be observed to be different to older JDK releases. These differences are mostly that the methods work correctly when previously they yield incorrect results or failed.
    • Java API
    • Implementation

      Summary

      Remove extended-length and UNC prefixes when normalizing java.io.File pathnames.

      Problem

      java.io.File instances constructed from path names beginning with the extended-length (long) path prefix "\?\" or the related universal naming convention (UNC) path prefix "\?\UNC\" [1] have inconsistent behavior with respect to Files created by equivalent path names without the prefix. For example, given the pathname \\?\C:\foo\bar containing a long path prefix, the method File.getCanonicalPath throws java.io.IOException: Bad pathname with the prefix in place, but returns the correct value, C:\foo\bar, if the prefix is first removed.

      [1] https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=registry

      Solution

      Strip the long path prefix \\?\ or UNC prefix \\?\UNC\ when the supplied path name is normalized. Initially it had been thought to strip the prefix in various individual methods, for example, getCanonicalPath, and isAbsolute, as appropriate, but it turns out that the path is already normalized by the time those methods are invoked.

      Specification

      There is no change to the specification.

            bpb Brian Burkhalter
            webbuggrp Webbug Group
            Alan Bateman
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: